Board index » delphi » Initialization / finalization in a DLL

Initialization / finalization in a DLL

Hi,

I'm trying to figure out how to free objects created in a DLL. There's no
Finalization section in a DLL. Where should this be implemented ?

Thanks in advance.

Stephan

 

Re:Initialization / finalization in a DLL


Hello Mike,

I am creating my objects between the begin/.end  of the DLL project file.
The code is correctly executed but because there's no finalization section
in the DLL project file, I could not free my objects and I was trying to
figure out how all this works. So you say that I should put both my
initialization and finalization code in another unit that my DLL project
file uses. Am I right ?

Thanks for your help.

Stephan

"Mike Williams (TeamB)" <mlwi!!iams@gmail!.com> a crit dans le message de
news: xn0f2epkl78rq46__BEGIN_MASK_n#9g02mG7!__...__END_MASK_i?a63jfAD$z__@newsgroups.borland.com...

Quote
> Stephan St-Denis wrote:

>> I'm trying to figure out how to free objects created in a DLL.
>> There's no Finalization section in a DLL. Where should this be
>> implemented ?

> The Initialization/Finalization sections go only in units.  Units may
> be part of a dll so you can perform actions in one of those.  When are
> the objects getting created?

> --
> -Mike (TeamB)

Re:Initialization / finalization in a DLL


Quote
Stephan St-Denis wrote:
> I'm trying to figure out how to free objects created in a DLL.
> There's no Finalization section in a DLL. Where should this be
> implemented ?

The Initialization/Finalization sections go only in units.  Units may
be part of a dll so you can perform actions in one of those.  When are
the objects getting created?

--
-Mike (TeamB)

Re:Initialization / finalization in a DLL


Quote
Stephan St-Denis wrote:
> So you say that I should put both my initialization and finalization
> code in another unit that my DLL project file uses. Am I right ?

That's certainly one option.  I like to keep create/destroy calls
paired up at the same level.  In other words, in normal windows apps,
if you create something in the FormCreate event then you should destroy
it in the FormDestroy event, etc.  Unit initialization/finalization is
likewise properly paired.  Your existing code which creates in the
project file actually doesn't execute until all unit initialization
sections have completed.

--
-Mike (TeamB)

Re:Initialization / finalization in a DLL


Mike,

Thanks for all the explanations and for your help.

I put all my create/free calls in a separate unit and everything works now.
No memory leaks.

Thanks again for your help. You saved me hours of searching because Google
didn't help on this one.

Regards,

Stephan

"Mike Williams (TeamB)" <mlwi!!iams@gmail!.com> a crit dans le message de
news: xn0f2eq4l79gerk__BEGIN_MASK_n#9g02mG7!__...__END_MASK_i?a63jfAD$z__@newsgroups.borland.com...

Quote
> Stephan St-Denis wrote:

>> So you say that I should put both my initialization and finalization
>> code in another unit that my DLL project file uses. Am I right ?

> That's certainly one option.  I like to keep create/destroy calls
> paired up at the same level.  In other words, in normal windows apps,
> if you create something in the FormCreate event then you should destroy
> it in the FormDestroy event, etc.  Unit initialization/finalization is
> likewise properly paired.  Your existing code which creates in the
> project file actually doesn't execute until all unit initialization
> sections have completed.

> --
> -Mike (TeamB)

Re:Initialization / finalization in a DLL


Quote
Stephan St-Denis wrote:
> I am creating my objects between the begin/.end  of the DLL project file.
> The code is correctly executed but because there's no finalization section
> in the DLL project file, I could not free my objects and I was trying to
> figure out how all this works. So you say that I should put both my
> initialization and finalization code in another unit that my DLL project
> file uses. Am I right ?

You might have read about the DllMain function that a DLL is expected to
have. Delphi exposes access to that function via the DllProc global
variable.

The DRP's begin-end block gets called in place of the initial call to
DllMain. It does not get called for other invocations of DllMain, but if
you have assigned a value to DllProc, that _will_ get called.

Write a function that matches the signature in the documentation, and
assign that function to DllProc in your DPR file. When that function
gets called, check the value of the Reason parameter. If it's equal to
Dll_Process_Detach, then your DLL is about to be unloaded.

--
Rob

Other Threads