P/Invoke DLL-memory managment

  • Thread starter Thread starter Yves Dhondt
  • Start date Start date
Y

Yves Dhondt

Hello,

I'm looking for information about the loading/unloading of unmanaged dlls.

Using the debugger I think it works as follows : the unmanaged DLL is
loaded as soon as a call to one of it's functions is made. After that,
it's kept in memory till the last call to it (by the managed program) is
done. After that it's unloaded.

Is this behaviour correct? Or is it possible that the DLL is unloaded
somewhere halfway my program? If I make a call to it than do
calculations for 4 days and then call it again would it stay loaded in
memory?

I was thinking about calling an initializing function which allocates
memory and use it later but i can't find any articles stating if that
would work or not (especially when the dll doesn't contain a
DllMain-function which is used to keep reference if i understand it well)

TIA

Yves
 
AFAIK once a DLL is loaded via P/Invoke it us nt unloaded until the process exits. Are you seeing smething different?

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hello,

I'm looking for information about the loading/unloading of unmanaged dlls.

Using the debugger I think it works as follows : the unmanaged DLL is
loaded as soon as a call to one of it's functions is made. After that,
it's kept in memory till the last call to it (by the managed program) is
done. After that it's unloaded.

Is this behaviour correct? Or is it possible that the DLL is unloaded
somewhere halfway my program? If I make a call to it than do
calculations for 4 days and then call it again would it stay loaded in
memory?

I was thinking about calling an initializing function which allocates
memory and use it later but i can't find any articles stating if that
would work or not (especially when the dll doesn't contain a
DllMain-function which is used to keep reference if i understand it well)

TIA

Yves
 
Unless you are explicitely unloading/unmapping the dll (by calling
FreeLibrary), it stays mapped into the process space until process shutdown.

Willy.
 
Not at all, just wanted to be sure about it, I couldn't find it back in the
docs.

Thanks

Yves
 
Back
Top