This project has moved. For the latest updates, please go here.

How to detect memory leaks in another dll?

Jun 12, 2010 at 3:40 PM

How to detect memory leaks in another dll?

I means, i want to detect all the memory leaks in my program. For example, the dll name is dlltest.dll, I set the configuration ini file: ForceIncludeModules = dlltest.dll. I only malloc some test memory in the dllmain entry. But, the code couldn't check the leak. The code looks like this:

1) main program:

#include <vld.h>

#include <windows.h>

#include <malloc.h>

int main(int argc, char *argv[])

{

  HMODULE hDll = NULL;

   char  *p1 = (char*)malloc(1);

  hDll = LoadLibrary("dlltest.dll");

 if(hDll!=NULL)

       FreeLibrary(hDll);

   return 0;

}

2) The dlltest.dll's code:

#include <windows.h>

#include <malloc.h>

BOOL DllMain(HINSTANCE hInstance,DWORD dwReason,void* pReserved)

{

   if(dwReason==DLL_PROCESS_ATTACH){

       char *p2 = (char*)malloc(1);

     }

     return TRUE;

}

 

Yes, the report output tells me that, there is a memory link at the line:  char *p1 = (char*)malloc(1), and print two line about loading the dlltest module and its debug symbols but no memory leak associate with it.

So, what should i do? Thanks

 

Coordinator
Jun 12, 2010 at 4:02 PM

You should include vld.h in dll code.

Jun 13, 2010 at 6:32 AM
Edited Jun 13, 2010 at 6:36 AM

Here are the two files. and i found another problem descripted at the end of my post. Thanks for your reply.

1)The main caller:

#include <Windows.h>
#include "../vld.h"

void main()
{
 char *p = new char[1];
 HMODULE hDll = LoadLibraryA("dlltest.dll");
 if(hDll)
  FreeLibrary(hDll);
 return;
}

2)The dlltest.dll's code:

#include "stdafx.h"
#include <stdio.h>
#include <malloc.h>

#include "../vld.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
 switch (ul_reason_for_call)
 {
 case DLL_PROCESS_ATTACH:
  {
   char *p = (char*)malloc(1);
  }
 case DLL_THREAD_ATTACH:
 case DLL_THREAD_DETACH:
 case DLL_PROCESS_DETACH:
  break;
 }
 return TRUE;
}

 The result is, the two modules myTest.exe and dlltest.dll are all dependending on vld_x86.dll. Maybe that's not the problem, the output likes:

==================================Dump start====================================================================

'MyTest.exe': Loaded 'D:\Study\OpenSource\PlugIns\vld-2.0\Win32\Debug\MyTest.exe', Symbols loaded.
'MyTest.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'MyTest.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'MyTest.exe': Loaded 'D:\Study\OpenSource\PlugIns\vld-2.0\Win32\Debug\vld_x86.dll', Symbols loaded.
'MyTest.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll'
'MyTest.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll'
'MyTest.exe': Loaded 'C:\WINDOWS\system32\secur32.dll'
'MyTest.exe': Loaded 'D:\Study\OpenSource\PlugIns\vld-2.0\Win32\Debug\dbghelp.dll'
'MyTest.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll'
'MyTest.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_597c3456\msvcr90d.dll', Symbols loaded.
Visual Leak Detector Version 2.0 installed.
    Forcing inclusion of these modules in leak detection: dlltest.dll
'MyTest.exe': Loaded 'D:\Study\OpenSource\PlugIns\vld-2.0\Win32\Debug\dlltest.dll', Symbols loaded.
'MyTest.exe': Loaded 'C:\WINDOWS\system32\apphelp.dll'
'MyTest.exe': Unloaded 'D:\Study\OpenSource\PlugIns\vld-2.0\Win32\Debug\dlltest.dll'
The thread 'Win32 Thread' (0x348) has exited with code 0 (0x0).
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 0 at 0x00576C90: 1 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\mytest\mytest.cpp (6): main
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582): __tmainCRTStartup
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): mainCRTStartup
    0x7C817077 (File and line number not available): RegisterWaitForInputIdle
  Data:
    CD                                                           ........ ........

Visual Leak Detector detected 1 memory leak.
ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 79 at 0x00532530: 33 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1615): Full call stack not available.
  Data:
    43 3A 5C 57    49 4E 44 4F    57 53 5C 73    79 73 74 65     C:\WINDO WS\syste
    6D 33 32 5C    41 44 56 41    50 49 33 32    2E 64 6C 6C     m32\ADVA PI32.dll
    00                                                           ........ ........

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 78 at 0x005324F0: 12 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1621): Full call stack not available.
  Data:
    76 6C 64 5F    78 38 36 2E    64 6C 6C 00                    vld_x86. dll.....

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 77 at 0x00532480: 60 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1615): Full call stack not available.
  Data:
    64 3A 5C 53    74 75 64 79    5C 4F 70 65    6E 53 6F 75     d:\Study \OpenSou
    72 63 65 5C    50 6C 75 67    49 6E 73 5C    76 6C 64 2D     rce\Plug Ins\vld-
    32 2E 30 5C    57 69 6E 33    32 5C 44 65    62 75 67 5C     2.0\Win3 2\Debug\
    76 6C 64 5F    78 38 36 2E    64 6C 6C 00                    vld_x86. dll.....

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 76 at 0x00532440: 13 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1621): Full call stack not available.
  Data:
    6B 65 72 6E    65 6C 33 32    2E 64 6C 6C    00              kernel32 .dll....

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 75 at 0x005323E8: 33 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1615): Full call stack not available.
  Data:
    43 3A 5C 57    49 4E 44 4F    57 53 5C 73    79 73 74 65     C:\WINDO WS\syste
    6D 33 32 5C    6B 65 72 6E    65 6C 33 32    2E 64 6C 6C     m32\kern el32.dll
    00                                                           ........ ........

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 74 at 0x005323A8: 10 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1621): Full call stack not available.
  Data:
    6E 74 64 6C    6C 2E 64 6C    6C 00                          ntdll.dl l.......

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 73 at 0x00532358: 30 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1615): Full call stack not available.
  Data:
    43 3A 5C 57    49 4E 44 4F    57 53 5C 73    79 73 74 65     C:\WINDO WS\syste
    6D 33 32 5C    6E 74 64 6C    6C 2E 64 6C    6C 00           m32\ntdl l.dll...

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 72 at 0x00532318: 11 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1621): Full call stack not available.
  Data:
    6D 79 74 65    73 74 2E 65    78 65 00                       mytest.e xe......

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 71 at 0x005322A8: 59 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (1615): Full call stack not available.
  Data:
    64 3A 5C 53    74 75 64 79    5C 4F 70 65    6E 53 6F 75     d:\Study \OpenSou
    72 63 65 5C    50 6C 75 67    49 6E 73 5C    76 6C 64 2D     rce\Plug Ins\vld-
    32 2E 30 5C    57 69 6E 33    32 5C 44 65    62 75 67 5C     2.0\Win3 2\Debug\
    4D 79 54 65    73 74 2E 65    78 65 00                       MyTest.e xe......

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 70 at 0x00532038: 576 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\tree.h (678): Full call stack not available.
  Data:
    01 00 00 00    00 00 40 00    FF AF 41 00    00 00 00 00     ......@. ..A.....
    18 23 53 00    A8 22 53 00    6C 3F 53 00    80 20 53 00     .#S.."S. l?S...S.
    A4 20 53 00    01 00 00 00    00 00 90 7C    FF 1F 9B 7C     ..S..... ...|...|
    00 00 00 00    A8 23 53 00    58 23 53 00    6C 3F 53 00     .....#S. X#S.l?S.
    80 20 53 00    6C 3F 53 00    01 00 00 00    00 00 80 7C     ..S.l?S. .......|
    FF 5F 8F 7C    00 00 00 00    40 24 53 00    E8 23 53 00     ._.|.... @$S..#S.
    38 20 53 00    6C 3F 53 00    5C 20 53 00    00 00 00 00     8.S.l?S. \.S.....
    00 00 20 03    FF 6F 2E 03    00 00 00 00    F0 24 53 00     .....o.. .....$S.
    80 24 53 00    6C 3F 53 00    38 20 53 00    6C 3F 53 00     .$S.l?S. 8.S.l?S.
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    EC 20 53 00    0D F0 AD BA     ........ ..S.....
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    10 21 53 00     ........ .....!S.
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    34 21 53 00    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     4!S..... ........

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 69 at 0x00533FD0: 8 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\tree.h (677): Full call stack not available.
  Data:
    00 00 00 00    38 20 53 00                                   ....8.S. ........

ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 68 at 0x00533F50: 80 bytes ----------
  Call Stack:
    d:\study\opensource\plugins\vld-2.0\vld.cpp (2445): Full call stack not available.
  Data:
    C8 20 53 00    98 3F 15 00    FF FF FF FF    00 00 00 00     ..S..?.. ........
    00 00 00 00    00 00 00 00    00 00 00 00    01 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    6C 3F 53 00    6C 3F 53 00    6C 3F 53 00     ....l?S. l?S.l?S.
    10 00 00 00    80 20 53 00    D0 3F 53 00    D0 3F 53 00     ......S. .?S..?S.

Visual Leak Detector is now exiting.
The program '[0xDF8] MyTest.exe: Native' has exited with code 0 (0x0).

====================================Dump END=================================================

Im not sure if i only include vld.h in the dll code. I tried once, it detected the memory leak in the dlltest module and lost the one in MyTest.exe, the main module. But, i couldn't try another time, because the process almost can't return from FreeLibrary. I trace the source code, i found it tracked by WaitForSingleObject( At line 556, the deconstructor function of  VisualLeakDetector, in vld.cpp)! I think, a deak lock occurred. Here, it's wating for the exit of the main thread, but the thread is its calling thread. Of course the calling will not exit, since the FreeLibrary can't return. It's waiting for itself, right? So, I think include vld.h in the dll code is not a perfect idea. There is another reason, if a project is dependent upon many other modules, the coder would have to modify all the code to include vld.h in every module.

Developer
Jul 29, 2010 at 2:22 PM
Hi, I wanted to do the same thing and I got tons of issues so I decided to have a go at the code and see what I could do about it. Here is my setup: A main exe taking DLL name in parameter and launching a specific function in the DLL. (Kcynice, that is about the same system described in your first post) I wanted to monitor all leaks in the DLL without having to add VLD to the DLL build. So my Main exe is linking with VLD and initializing it. So here is what I have added. - VLDRefreshModules: will look what new modules have been loaded and patch them accordingly. - VLDEnableModule: Enable a specific module. This will turn on memory leak detection on the module without the need to specify it in the ini file. - VLDReportLeaks : report all leaks (and also added report to stdout ) And it seems to be working great now. All that tested under VS 2008 x64 debug for now. Since I encountered a lot of other probably unrelated issues (Infinite Loops and weird crash due to incorrect GetProcAddress return) I am not willing to commit the code for that yet. Ideally KindDragon would contact me directly and we would exchange the code. I am likely to use a version with my modifications in 1 or 2 other projects (in addition to the one already tested) so I would prefer to use an updated version from CodePlex rather than my own version :)