I am currently reviewing a legacy code where the CDynLinkLibrary is used and I am analyzing the code in an analysis tool (checkmarx) that reports vulnerabilities in it. The code is implemented as shown on your page, except that when creating the object of type CDynLinkLibrary, a pointer to this object is stored in the lib variable and it is at this point that I get memory leak. Is it normal for this to happen?
static AFX_EXTENSION_MODULE TestDll1DLL = { NULL, NULL };
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("TESTDLL1.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(TestDll1DLL, hInstance))
return 0;
// comments deleted
CDynLinkLibrary *lib = new CDynLinkLibrary(TestDll1DLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("TESTDLL1.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(TestDll1DLL);
}
return 1; // ok
}
I have tried to free the memory using delete lib, however using this option the program stops working. I have also found a tip that many leak detection techniques will try too soon before these objects can be released.