Var
The List: TObjectList
; Obj: TNewObject; The begin Obj:=TNewObject. Create (" Three "); List:=TObjectList The Create (); List. The Add (TNewObject. Create (' One ')); List. The Add (Obj); Obj. Free;//hand release List. Pack (function (const L, R: TNewObject) : Boolean begin result:=L=nil; End)//how to judge whether L release List. The Free Readln; End. The code above, Originally TObjectList to delete one of the elements, automatic release the deleted element, But this element once removed at outside, so the List. The Free time, will be an error, Therefore, need to clean up the List, that is, Pack, clean up empty element, Need in the Pack, then define what kind of element is an empty element, involves the judgment of L, If L=nil then... If Not a Boolean (L) then... If Assigned (L) then... The three way, cannot judge Obj is Free,,, Beg people! CodePudding user response:
You can List. OwnsObjects:=false; CodePudding user response:
reference 1st floor DelphiGuy response: can List. OwnsObjects:=false; If must be in the List. OwnsObjects:=True; This, how to judge the Obj was Free off? CodePudding user response:
refer to the second floor rockmmm response: Quote: refer to 1st floor DelphiGuy response: You can List. OwnsObjects:=false; If must be in the List. OwnsObjects:=True; This, how to judge the Obj was Free off? Can write a helper for TNewObject type, replaced free method, in check List, but from a design perspective, this is a very bad design, forcibly associated external objects, increase the coupling, CodePudding user response:
reference DelphiGuy reply: 3/f Quote: refer to the second floor rockmmm response: Quote: refer to 1st floor DelphiGuy response: You can List. OwnsObjects:=false; If must be in the List. OwnsObjects:=True; This, how to judge the Obj was Free off? Can write a helper for TNewObject type, replaced free method, in check List, but from a design perspective, this is a very bad design, forcibly associated external objects, increase the coupling, You said this way, I also tried, but it's not there is a helper, its role is only used to judge the existence of obj and in addition, you said, from the Angle of the design of this kind of design is very common, for example with three ObjectList obj, employees a list, a store employee, the last remaining all personnel, there must exist a obj by multiple reference list, when one list deleted after a certain obj list. The other free time, will be a problem, CodePudding user response:
TObjectList objects have OwnsObjects property, when set to True, TObjectList will destroy any object is removed from the list of objects, CodePudding user response:
reference 4 floor rockmmm response: Quote: reference DelphiGuy reply: 3/f Quote: refer to the second floor rockmmm response: Quote: refer to 1st floor DelphiGuy response: You can List. OwnsObjects:=false; If must be in the List. OwnsObjects:=True; This, how to judge the Obj was Free off? Can write a helper for TNewObject type, replaced free method, in check List, but from a design perspective, this is a very bad design, forcibly associated external objects, increase the coupling, You said this way, I also tried, but it's not there is a helper, its role is only used to judge the existence of obj and in addition, you said, from the Angle of the design of this kind of design is very common, for example with three ObjectList obj, employees a list, a store employee, the last remaining all personnel, there must exist a obj by multiple reference list, when one list deleted after a certain obj list. The other free time, will be a problem, Since you demand is can release the object itself, can make its owners (such as a list) release, also can replace Free with a helper method to deal with, what I mean is to use a helper object to process the release of synchronization is not a good design, of course, you this kind of demand itself is not a good design, if it is not support class helpers function of language, like c + +, this kind of demand will not be able to realize, only through reasonable design to solve, such as rules or manual release object, or the owner shall be responsible for the release, cannot mix,