[DUG] Usage - initialization and finalization
Paul A Norman
paul.a.norman at gmail.com
Fri May 19 16:04:15 NZST 2006
Thanks for all of that everybody.
Kyely, the information on scope, and also the non-existance yet of the
Application object during *Initialization* is very helpful thanks.
"You don't always have to use initialization to create and finalize to
destroy. That would be very resource hungry."
I was thinking of it as an alternative, like *Initialization *as the point
to create gloabl UNIT wide VAR .TstringLists etc .. instead of during
.onFormCreate, and for freeing stringlists etc .. during
*Finalization*instead of during .
*onDestroy*
Reason being . .
I was wondering whether there are any circumstances where using Finilization
to .*free* objects is more reliable than .*ondestroy *?
Is Finalization called after Tform.*onDestroy*, or does it failsafe even if
onDestroy fails for some system reason?
Paul
On 19/05/06, Jeremy North <jeremy.north at gmail.com> wrote:
>
> 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
> Delphi at ns3.123.co.nz
> http://ns3.123.co.nz/mailman/listinfo/delphi
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ns3.123.co.nz/pipermail/delphi/attachments/20060519/2a039172/attachment.html
More information about the Delphi
mailing list