I build two c++ projects in visual studio 2015. A dll project (myFunction.dll) that implements a function (myFunction). An exe project which simply calls that dll function.
I have a memory leak in dll project (a new without delete in dllmain.cpp) like this:
dllmain.cpp (initilization() under case DLL_PROCESS_ATTACH has memory leak)
#include "stdafx.h"
#include <iostream>
#include "TestHelper.h" //header file for a initialization function called when dll process attaches
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
initialization(); //implementation of this function has a new without matching delete
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
Then I was trying to debug my exe project to detect memory leak.
1). If I create memory check points before and after my call to dll function and compare the difference as follows, then visual studio reports no difference, i.e., no memory leaks;
#include "myFunction.h" //header for myFunction.dll
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
int main()
{
_CrtMemState s1;
_CrtMemState s2;
_CrtMemState s3;
_CrtMemCheckpoint(&s1);
myFunction(); //call to dll library whose entry point function causes deallocated memory on the heap
_CrtMemCheckpoint(&s2);
if (_CrtMemDifference(&s3, &s1, &s2))
_CrtMemDumpStatistics(&s3); // no memory difference found; thus no leak
return 0;
}
2). If I call _CtrDumpMemoryLeaks() at the exit of my exe project, then the memory leak from my dll project was captured and reported.
#include "myFunction.h" //header for myFunction.dll
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
int main()
{
myFunction(); //call to dll library whose entry point function causes deallocated memory on the heap
_CrtDumpMemoryLeaks(); //captures and reports memory leak from dll library
return 0;
}
My question is why isn't the first method capturing memory leak in my dll library if there is clearly new allocated heap memory at checkpoint s2? Shouldn't it be equivalent to the second method? or there is subtle difference between these two when it comes to finding memory leaks from dll entry point functions?