Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Is there any way to write a function that will be called when my DLL is unloaded? One thing I can think of is creating a global object in my DLL and writing the memory free calls in its destructor</p> </blockquote> <p>That's possible, although I believe exactly when your object's destructor will be called will be undefined.</p> <p>You might be interested in <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583.aspx" rel="noreferrer"><code>DLL_PROCESS_DETACH</code></a>, and although you should avoid doing anything significant in <code>DllMain</code>, it seems deallocating resources is acceptable here. Note the caveats:</p> <blockquote> <p>When a DLL is unloaded from a process as a result of an unsuccessful load of the DLL, termination of the process, or a call to FreeLibrary, the system does not call the DLL's entry-point function with the DLL_THREAD_DETACH value for the individual threads of the process. The DLL is only sent a DLL_PROCESS_DETACH notification. DLLs can take this opportunity to clean up all resources for all threads known to the DLL.</p> <p>When handling DLL_PROCESS_DETACH, a DLL should free resources such as heap memory only if the DLL is being unloaded dynamically (the lpReserved parameter is NULL). If the process is terminating (the lpvReserved parameter is non-NULL), all threads in the process except the current thread either have exited already or have been explicitly terminated by a call to the ExitProcess function, which might leave some process resources such as heaps in an inconsistent state. In this case, it is not safe for the DLL to clean up the resources. Instead, the DLL should allow the operating system to reclaim the memory.</p> </blockquote> <p>You might need to elaborate on <em>why</em> your DLL can hold on to memory, if you have numerous objects created by the DLL, they should have a defined lifecycle and clean themselves up at the end of their life.</p> <p>If they're not objects (i.e. memory being allocated and returned to the caller via functions) why not put the responsibility back onto whoever is consuming your DLL? They can free the memory. The Terminal Services library follows this pattern (<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa383834.aspx" rel="noreferrer"><code>WTSFreeMemory</code></a>).</p> <p>If the resources are long-lived and <em>must</em> exist for the lifetime of your library, let the consumer control the lifecycle of your library. Write two functions: <code>MyFrameworkStartup</code> and <code>MyFrameworkShutdown</code> as appropriate. Winsock follows this pattern (<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms742213.aspx" rel="noreferrer"><code>WSAStartup</code></a> and <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms741549.aspx" rel="noreferrer"><code>WSACleanup</code></a>).</p> <blockquote> <p>My other idea is to just rely on the operating system to free the memory when the DLL unloads, but this seems dirty.</p> </blockquote> <p><a href="http://blogs.msdn.com/b/oldnewthing/archive/2012/01/05/10253268.aspx" rel="noreferrer">You'll be okay if the process is exiting</a>:</p> <blockquote> <p>Don't worry about freeing memory; it will all go away when the process address space is destroyed. Don't worry about closing handles; handles are closed automatically when the process handle table is destroyed. Don't try to call into other DLLs, because those other DLLs may already have received their DLL_PROCESS_DETACH notifications, in which case they may behave erratically in the same way that a Delphi object behaves erratically if you try to use it after its destructor has run.</p> </blockquote> <p>Make sure you read the whole article and comments and understand it before implementing the "do nothing" strategy.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload