Shutdown Handling: DLL & Shared Object Detachment?

Sergey Gromov snake.scaly at gmail.com
Mon Sep 1 13:04:14 PDT 2008


Benji Smith <dlanguage at benjismith.net> wrote:
> But, not knowing much about DLL programming, I don't know whether a host 
> application is required to unload the DLL, or whether it's more common 
> for the host application's process to just terminate without ever 
> detaching from the DLL. If the process terminates suddenly, or if the 
> programmer of the host application neglects to unload the DLL, does the 
> OS call the unload function?

In Windows, you must implement DllMain() anyway.

http://msdn.microsoft.com/en-us/library/ms682583.aspx

This function is called with a *reason*. It could be DLL_PROCESS_ATTACH, 
DLL_PROCESS_DETACH, and some others. The ATTACH is called whenever a DLL 
is loaded into the host process's address space. The DETACH is called 
whenever a process stops using DLL for whatever reason, including 
failure to load DLL and process termination. The reason of calling 
DETACH is also specified.

The problem is, if DETACH is called because a process terminates, the 
order of uninitializing process DLLs is unknown, not unlike the order of 
collecting objects by GC is unknown. It may happen that sockets library 
is already uninitialized. Basically you cannot do anything sophisticated 
in DllMain which is discussed in the above article.

There are no other standard ways to know if the host process is 
terminating.

I know nothing about Unix/Mac shared objects.

-- 
SnakE


More information about the Digitalmars-d-learn mailing list