The next question is this question : (note that this is not a duplicate, I ask for alternatives here).
Is there any way to do the following work:
type List <T> = record private FList : TList <T>; FGuard : IInterface, procedure CheckCreated; public procedure Add(const Value : T); end; procedure List <T>.CheckCreated; begin if (FGuard = nil) then begin FList := TList <T>.Create; FGuard := TGuard.Create (FList); // guard calls free on list in destructor end; end; procedure List <T>.Add (const Value : T); begin CheckCreated; FList.Add (Value); end;
Ideally, I want to use it as follows:
function ReturnHandles : List <THandle>; begin Result.Add (2); Result.Add (3); end;
As explained in the answers to a related question, this does not work (which is really useless). He will not create a new list for each call.
Unfortunately, this does not work either:
function ReturnHandles : List <THandle>; begin Initialize (Result); Result.Add (2); Result.Add (3); end;
It removes security interfaces and all lists because Initialize simply rewrites the link to the interface without decreasing the link count.
Is there any way to make this work? Or would you suggest making this interface instead of recording and just living with a construction line?
function ReturnHandles : List <THandle>; begin Result := List <T>.Create; Result.Add (2); Result.Add (3); end;
Thank you for your help!
source share