delphi - How to free object contained in another object -


I am using Delphi 2010 and I have a question of reclamation of the object.

And if there is an exception, then I want to clean both TProduct and TPart. How do I properly tie the object contained?

  // The clause is defined as this TProduct = class (trimotable) Private F name: string; FAge: integer; FColor: string; FPart: TPart; Published property name: string FName type FName; Property Age: Read Integer FAge Write FAge; Property color: FString FString to read string; Property Part: TPart FPart Write FPart; End; TPart = Class (Trustable) Private F Name: string; FColor: string; Published property name: string FName type FName; Property color: FString FString to read string; End; // Then in a method, the code will get information from the database, MyFunction to create objects and fill them functions: TProduct; Results start: = zero; Query.Open; Results: = TProduct.Create; Try the result. Name: = qery.FieldByName ('NAME'). AstString; Result.Age :. = Query.FieldByName ('age') AsInteger; Result.Color :. = Query.FieldByName ('COLOR') AsString; ... if (Result.Color = 'Blue') then the result starts. Part: = Part. Create; Result.Part.Name:='Bottle '; Result.Part.Color: = 'Green'; End; Except E: Exceptions start // Exception, I want to make sure that both TProduct and TPart objects are properly free and Neil (results); // Work fine fine (part result); // & lt; - Will not be compiled - "Continuous object can not be passed as var parameter" part. free; // & lt; - Compiels but free and blue is not good? End; End; After all, Curie. Stop it; End; End;   

I am not trying to redesign the code, only to ensure that if TPart has been created, then it is also free?

The correct way is to own A B Similarly, your exception handler is only meant to worry about evacuating A , and it is A s distructor free code> B if created before the exception was made.

  type class A = class public B: class B; Destruction of Destruction; Override; End; District Class A Destost; Start Beepree; inherited from; End;   

  var A: ClassA; Starting ... A: = ClassA.Create; Try ... AB: = Class B Create; ... Except e: Exception begins free and Neil (A); // or free; ... End; End; ... End;    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -