473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

DLL Memory Leaks - What Do I Do?

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: ra******@beckman.com
== Email 2: ra******@mindspring.com
==
==
== 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."
==
================================================== ====================
================================================== ====================
Nov 17 '05 #1
3 6385
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;
}
--

This posting is provided "AS IS" with no warranties, and confers no rights.
"Robert A Riedel" <ra**********@beckman.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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.
...
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.

Nov 17 '05 #2
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.

"S. Han" <@> wrote in message news:40********@news.microsoft.com...
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;
}
--

This posting is provided "AS IS" with no warranties, and confers no rights.

"Robert A Riedel" <ra**********@beckman.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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.
...
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.


Nov 17 '05 #3
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.
--------------------

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Maurice | last post by:
Hi there, I'm experiencing big memory problems on my webserver. First on an old RedHat 7.2 system, now on an other fresh installed Suse 8.2 system: Linux version 2.4.20-4GB...
0
by: Steve Binney | last post by:
My code makes synchronous HttpWebRequest and HttpRebResponse calls. In VS 2003, I am getting memory leaks and event handle leaks. I am closing all streams and using "using"statements. I have...
4
by: Morten Aune Lyrstad | last post by:
Ok, now I'm officially confused. I have a large project going, which uses a win32 ui library I am developing myself. And I'm getting weird memory leaks. I don't know if I can explain what is going...
2
by: Generic Usenet Account | last post by:
I have been using STL for a long time now, without any problems. Recently we generated a purification report on our software using Rational Purify, and we found some memory leaks. My colleague...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
0
by: Frank Lopez | last post by:
Does anyone know if Microsoft generated a whitepaper on this topic? Does anyone know what the solution is? (meaning, eliminate the leak problem -- I am seeing three memory leaks from...
4
by: ali.jan | last post by:
Hi, It is trivial to load an assembly in a new Application Domain. Is there any way of loading an assembly in a new process? I tried using the Process class like this: Process p = new...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
16
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.