DLL Memory Leaks - What Do I Do?

R

Robert A Riedel

I am having a difficult time solving a memory leak in a DLL. The problem
occurs because I cannot find a way to control the termination of threads
managed by the DLL.

In a thread procedure that is part of the DLL data segment there is an
allocation that occurs from the heap. This heap allocation is leaked when a
process using the DLL exits because the DLL CRT startup and termination code
calls ExitProcess(), which terminates all threads.

The DLL code itself does not start the thread. Code in the client process
starts threads by calling a DLL function.

Is there any way to get notification of the process exit before the CRT
calls ExitProcess? I tried atexit(), but this also appears to execute after
the CRT ExitProcess call.

Or perhaps the thread procedure needs to be in the EXE data segment instead
of the DLL data segment?

--
======================================================================
======================================================================
==
== Bob Riedel
== Beckman Coulter, Incorporated
== PO Box 8000 W-529
== 200 S Kraemer Blvd
== Brea CA 92822-8000
==
== Email 1: (e-mail address removed)
== Email 2: (e-mail address removed)
==
==
== The opinions expressed are my own, and do not necessarily represent
== those of Beckman Coulter, Inc.
==
======================================================================
======================================================================
==
== "Effective education is the key to successful democracy."
==
== "Criticizing the actions of others offers so little risk, and
== requires so little effort that it is, without exception, the tool
== of the lazy and of the foolish -- who have neither the intelligence
== to discover, nor the discipline to pursue, a realistic
== alternative."
==
======================================================================
======================================================================
 
S

S. Han

According to my Win32 programming manual, "...if a thread is terminated
by ExitProcess, the DLL entry point functions are invoked once, to indicate
that the process is detaching."

So I think you should be able to get a notification in your DllMain and
should be able to run a clean up code like this:

// Dll entry point
BOOL WINAPI DllMain(HINSTANCE hInstance,
DWORD fdwReason,
PVOID pvReserved)
{
switch (fdwReason){
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
Cleanup(); // Do your clean up here.
break;
}
return TRUE;
}
 
R

Robert A Riedel

I appreciate the input, however, what you are suggesting will not help for
resources allocated within the scope of a thread procedure. The call to
DllMain is made by the CRT, which is outside the scope of the allocating
thread procedure. In order to clean up the resources allocated within the
thread procedure, it must run to completion. The only other way to
accomplish this feat is to introduce coupling between resources within the
thread procedure and DllMain, which is not desirable for several reasons.

What I need is to be able to intercept the beginning of process shutdown
before the CRT makes the ExitProcess call in order to notify running threads
of the impending shutdown so they can terminate normally.
 
G

Gary Chang

Hi Robert,

Thanks for posting in the community.
Is there any way to get notification of the process exit before the CRT
calls ExitProcess?

Maybe not.


However, can you take a smart pointer-like way to capsulate your memory
allocation in a class within your worker thread, an do the memory clean-up
works in the class' destructor function, I think it would be executed when
the thread terminating.

BTW, sometimes there may be some fake memory leak cases:
http://support.microsoft.com/?id=167929


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top