Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

No call of "initialization / finalization" in BPL?

513 views
Skip to first unread message

Mario Werner

unread,
Mar 14, 2005, 10:17:38 PM3/14/05
to

Hi there,

following a tutorial I swapped MDIChild forms in a BPL and load them dynamically into my program. This works quit fine so far.

But I discovered that it seems that there is no call of the initialization / finalization blocks in any unit of the BPL package. So this causes variables and objects to be initialized there are just dead which in turn causes problems in the further program flow.

To debug I set a breakpoint on a call in the initialization block which was marked as beeing OK and active but the program never stopped there. Then I put a "ShowMessage ('Hello?');" there which also not appeared.

Why is it like that and how can I correct this?! oO

Kind regards,
Mario

Rob Kennedy

unread,
Mar 15, 2005, 1:56:34 AM3/15/05
to
Mario Werner wrote:
> But I discovered that it seems that there is no call of the
> initialization / finalization blocks in any unit of the BPL package.
> So this causes variables and objects to be initialized there are just
> dead which in turn causes problems in the further program flow.
>
> To debug I set a breakpoint on a call in the initialization block
> which was marked as beeing OK and active but the program never
> stopped there. Then I put a "ShowMessage ('Hello?');" there which
> also not appeared.

If the operating system loads the BPL as part of loading the associated
EXE, then not all the initialization sections will get called. Instead,
only the sections from the units that are explicitly used by something
else in the program get called.

If the code in the initialization section registers a class, and then
you only refer to that class indirectly, say by looking for it by name
in a list, then the unit's initialization section might not get called.
Adding that unit to any "uses" clause in your program should solve that
problem.

To work around this problem, you can initialize the package's units
yourself by calling the InitializePackage function, in the SysUtils
unit. It requires a module handle, which you can get by calling the
GetModuleHandle API function. That function will only call the
initialization sections of the units that haven't already been
initialized. That's my observation, anyway.

If you call InitializePackage, then you should also call
FinalizePackage. When your package gets unloaded, the finalization
sections will get called for all the units that were automatically
initialized.

If the OS does _not_ automatically load your package, then you are
loading it with the LoadPackage function. It initializes all the
package's units for you, so you don't need to call InitializePackage
yourself. Likewise, UnloadPackage will finalize everything for you.

--
Rob

Mario Werner

unread,
Mar 15, 2005, 11:27:22 AM3/15/05
to

Hello Rob,

thanks alot! :) :) InitializePackage / FinalizePackage is the solution and now everything works as expected. :) *glad and happy*

Best wishes,
Mario

0 new messages