MFC DLL load from Win32 App -> Memory Leaks  
Author Message
eman1975





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

I hope i'm clear to post the problem.  I'm using Visual Studio 2003 SP1. The main application is a simple Win32 application that link dinamically a MFC dll. In some case when a try to execute FreeLibrary of the dll previusly loaded by LoadLibrary i get a memory leaks. For example in this code :

a=new int [100];
hmod=LoadLibrary(FILEDLL);
FreeLibrary(hmod);
delete [] a;

when i execute the FreeLibrary i get this memory dump:

"TestLoadFree.exe": Load"C:\.......\TestMemLeaks.dll", ....
"TestLoadFree.exe": Load"C:\WINDOWS\system32\mfc71d.dll", ....
"TestLoadFree.exe": Load"C:\WINDOWS\system32\MFC71ITA.DLL", ....

Detected memory leaks!
Dumping objects ->
{60} normal block at 0x00852C88, 400 bytes long.
Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
"TestLoadFree.exe": Unload"C:\.......\TestMemLeaks.dll"
"TestLoadFree.exe": Unload"C:\WINDOWS\system32\mfc71d.dll"
"TestLoadFree.exe": Unload"C:\WINDOWS\system32\MFC71ITA.DLL

 

It seems a Fake memory leaks .. but i'm not sure. ! So i added the ExitInstance handler of MFC dll to verify when the memory leaks is fired:

int CTestMemLeaksApp::ExitInstance()
{
  TRACE(_T("Start ExitInstance() for regular DLL: TESTDLL\n"));
  int retvalue=CWinApp::ExitInstance();
  TRACE(_T("End ExitInstance() for regular DLL: TESTDLL\n"));
  return retvalue;
}

and i get this memory dump:

"TestLoadFree.exe": Load"C:\.......\TestMemLeaks.dll", ....
"TestLoadFree.exe": Load"C:\WINDOWS\system32\mfc71d.dll", ....
"TestLoadFree.exe": Load"C:\WINDOWS\system32\MFC71ITA.DLL", ....

Start ExitInstance() for regular DLL: TESTDLL
End ExitInstance() for regular DLL: TESTDLL


Detected memory leaks!
Dumping objects ->
{60} normal block at 0x00852C88, 400 bytes long.
Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
"TestLoadFree.exe": Unload"C:\.......\TestMemLeaks.dll"
"TestLoadFree.exe": Unload"C:\WINDOWS\system32\mfc71d.dll"
"TestLoadFree.exe": Unload"C:\WINDOWS\system32\MFC71ITA.DLL"

the deb**** write the memory dump when the FreeLibrary is called ...
It is clear that the memory leaks is related to a pointer allocation. But it is not clear because the memory leak is present! I suspect to some not right settings of two project... But the main application is a Win32 application with Code generation = Debug Multithreaded DLL, while the dll mfc use the same code generation setting and the MFC is used in shared mode (Use MFC in Shared DLL). I think it is all right .. but the memory leaks is again there!

If it is useful i can post the entire two project (both are near to the code generate by wizard  )

Thank in advance!

 

 

 




Visual C++7  
 
 
einaros





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

Try using

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

instead of dumping the memory leaks by direct calls to _CrtDumpMemoryLeaks(). Setting those two flags will automatically dump memory leaks when your application terminates, and will not be affected by global objects or allocations made within the same scope as where _CrtDumpMemoryLeaks would otherwise be called.



 
 
eman1975





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

Thanks for your answer... i don't execute any _CrtDumpMemoryLeaks() explicit calling, anyway i insert

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

at main program begining (_tWinMain) but the memory leak is again there!! !! I try to insert the crt debug flag setting before the LoadLibrary calling but same result!

Does the MFC Framework call _CrtDumpMemoryLeaks() in implicit way

Thanks!



 
 
eman1975





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

I compiled Main Application and the DLL with VS2005 ... and i haven't the the memory leack that i've with VS2003 ...

Will it fixed in vs2003

Thanks.



 
 
Simple Samples





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

A Memory Leak occurs when memory is allocated but never unallocated. They occur during the execution of the program but are detected when the program exits. It is usually very difficult for people in this forum and other forums and in newsgroups to help find a memory leak since it is usually necessary to look at the entire program to find the problem. Note that einaros is trying to help you diagnose the problem. We don't know enough about your program to find the problem.

See Memory Leak Detection in MFC and the relevant portions of Debugging in Visual Studio to diagnose the problem.



 
 
eman1975





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

The problem is that the memory leak was generated (seemsly) by MFC Framework. I created a couple DLL+EXE and i can provide the code if it needed to resolve my problem.

 

 

Thanks a lot for your help.



 
 
Simple Samples





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

 
The problem is that the memory leak was generated (seemsly) by MFC Framework.
That might be, but it is unlikely. If MFC were to have a bug like that, then it is probable that there are many others that would encounter it and report it. It is more likely that it was fixed by something you did, even if you are not aware of it. So for future reference, don't assume it is MFC.

The best thing to do is to in the future always ensure that memory that is allocated is freed. The latest improvements to C++ include gcnew, which can solve the problem of freeing memory explicitly. I am not familiar with the details but I think it would help you. Ideally though it is better to design the program so that allocated memory is freed explicitly.



 
 
Viorel.





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

[...]

a=new int [100];
hmod=LoadLibrary(FILEDLL);
FreeLibrary(hmod);
delete [] a;

[...]


Detected memory leaks!
Dumping objects ->
{60} normal block at 0x00852C88, 400 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

Based on the size of detected memory leak -- 400 -- it seems the memory block belongs to your a array which was not yet deleted when the regular DLL performs an internal checking for leaks on exit.

If you change the order of operations to:

a=new int [100];

hmod=LoadLibrary(FILEDLL);

delete [] a;

FreeLibrary(hmod);

and do not receive messages anymore, then I think it was a false alarm. To be sure you do not have other leaks in your executable, try _CrtDumpMemoryLeaks() at exit.


 
 
Simple Samples





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

I forgot to mention that the value CD CD CD indicates that the memory probably was not initialized, or was copied from uninitialized memory. Debug builds perform some initializations that release builds do not, but the initialization uses specific values that help us identify problems.

 
 
einaros





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

As somewhat of a side note; Marius Bancila wrote an excellent article on memory management and the debug build (including the meaning of the different memory values). See http://www.codeguru.com/cpp/w-p/win32/tutorials/article.php/c9535/.

 
 
eman1975





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

Thanks for your answers...

I read the article at the codeguru site. It's very good to know how memory allocation/deallocation is done via new/delete or malloc/free. But i need to know if FreeLibrary call the _CrtDumpMemoryLeaks() in implicit way. And if there is a compiling option to disable this behavior. The memory leak vanishes if i insert the delete between LoadLibrary and FreeLibrary, but i can't do it in my main project.



 
 
Simple Samples





PostPosted: Visual C++ General, MFC DLL load from Win32 App -> Memory Leaks Top

FreeLibrary does not call _CrtDumpMemoryLeaks() in any way, but even if it did, disabling it would solve nothing useful.