Thursday, May 14, 2009

Initialization and Finalization Sections of a Unit!

"Initialization & Finalization Sections - What are they?" - Ever heard of these names earlier while programming delphi application, must have but never bothered much about them.. Guess Right??..

Yes, these two terms are the optional sections of a delphi unit, the common one being the unit header, interface section and implementaion section. However, sometimes we are in a need to perform certain tasks before any of the units code is executed, it is then that we start searching for these two optional sections.

Wheneever we need to do some extra work before any other code is read we place that piece of code in the Initialization section of the unit which is then being called while the application is executed. Now, once this is done and the application needs to terminate then all the resources allocated during the Initialization section of a unit are done free in the finalization section. These two are being declared like this -

unit

interface

implementation

initialization
{Place initialization code here}
finalization
{Place finalization code here}
end.

i.e. before the final end of the unit.