[DUG] Usage - initialization and finalization
Kyley Harris
kyley at harrissoftware.com
Fri May 19 13:52:45 NZST 2006
Initialization and finalization are the first and last pieces of code
ever called. They are executed in dependency order first, and in reverse
of that for finalization.
So unit a uses unit b = b.init then a.init.
You can do anything at all in initialization where the code has scope.
So you wouldn't use the Application Varaiable from here, because it
hasn't been created.
You don't always have to use initialization to create and finalize to
destroy. That would be very resource hungry.
Many times I would do something similar to this.
Function GetGlobalWhateverList:TList;
Begin
If globallist = nil then
Globallist := TList.Create;
Result := globallist;
End
Initialization
Globallist := nil;
Finalization
FreeAndNil(GlobalList)
You simply have to be aware of what exists in memory at this point in
time.
________________________________
From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz]
On Behalf Of Paul A Norman
Sent: Friday, 19 May 2006 12:59 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] Usage - initialization and finalization
Point of interest - something basic for me and possibly others to learn.
I've only ever used unit 'initialization and finalization' "blindly"
since Delphi 3 days for Activex / OLE type stuff, as just someting that
you have to do - without understanding.
initialization
OleInitialize(nil);
finalization
OleUninitialize;
Otherwise I've used the onFormComplete and onDestroy to set intial stuff
up and then release stuff.
But I am guessing that Finailization is a better place to release stuff
(help avoid memoery leakage under certain circumstances?), but
Initialization would have to be used for Finalization to be acceptable..
I've read the Delphi 2005 help and googled a bit, but am not yet sure
just what sorts of code can be put in the Initialization and
Finalization.
1. Can anything declared as a unit VAR or CONST be dealt with or
used in Initialization ? it appears so to me from what I see (below).
2. Could I safely .create and .free objects respectively?
I was reading through a Unit the other day and saw all sorts of things
in Initialization there that surprised me.
Can any one please point me to a good reference on this, or in our new
learning sharing attitude, explain it for every ones benefit here?
initialization
FillChar(BrowserData,SizeOf(BrowserData),#0);
OleInitialize(nil);
New(PtrWGUID);
New(PtrMGUID);
New(PtrDGUID);
PtrWGUID^:=CGID_WebBrowser;
PtrMGUID^:=CGID_MSHTML;
PtrDGuid:=PGUID(nil);
GetBrowserData(BrowserData);
IEStr:=IEVerStr;
OnMessageCompNo:=0;
finalization
Dispose(PtrWGUID);
Dispose(PtrMGUID);
Dispose(PtrDGUID);
OleUninitialize;
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ns3.123.co.nz/pipermail/delphi/attachments/20060519/d24a6c57/attachment-0001.html
More information about the Delphi
mailing list