An alternate and thread safe way to perform this operation is to use InterlockedCompareExchange.
It is possible (although not very probable) to have FList created twice without using InterlockedCompareExchange. Probably not worth the extra effort though. ms-help://borland.bds4/dllproc/base/interlockedcompareexchange.htm This technique is implemented in the TPrivateHeap class in the PrivateHeap unit. It was originally written by Hallvard Vassbotn. If you are interested in the inner workings of Delphi and the Compiler, you should read his blog (http://hallvards.blogspot.com/). For the OP I would minimize the use of the Initialization section to cases where you really need it. Otherwise your unit cannot be smart linked out. /// code below unit Unit4; interface uses Windows , Classes , SysUtils ; function GetList: TList; implementation var FList: TList; function GetList: TList; var lList: TList; begin if FList = nil then begin lList := TList.Create; if InterlockedCompareExchange(Integer(FList), Integer(lList), 0) <> 0 then lList.Free; end; result := FList; end; initialization finalization FreeAndNil(FList); end. _______________________________________________ Delphi mailing list [email protected] http://ns3.123.co.nz/mailman/listinfo/delphi
