OK. So this memory leak occurs randomly. And I tried to reproduce it in a simplified version of my original program. It's a VC++ console application with common header files for MFC.
The following is my memManager class, which originally is a image correction class that manages memory and also processes image.
typedef struct Point
{
int x;
int y;
}TPoint;
class memManager
{
public:
memManager();
~memManager();
void AllocMemory(TPoint **pAddr, int nSize);
void ReleaseMemory(TPoint **pAddr);
void ReleaseAllMemory();
TPoint* ptsPtr;
};
memManager::memManager()
{
ptsPtr = NULL;
}
memManager::~memManager()
{
ReleaseAllMemory();
}
void memManager::AllocMemory(TPoint **pAddr, int nSize)
{
if (*pAddr != NULL)
return;
TPoint *pTempAddr;
pTempAddr = new TPoint[nSize];
memset(pTempAddr, 0, sizeof(TPoint)*nSize);
*pAddr = pTempAddr;
return;
}
void memManager::ReleaseMemory(TPoint **pAddr)
{
if (*pAddr != NULL)
{
delete[] * pAddr;
*pAddr = NULL;
}
return;
}
void memManager::ReleaseAllMemory()
{
ReleaseMemory(&ptsPtr);
return;
}
And here is the the code in the main function.
memManager myMemManager;
myMemManager.AllocMemory(&myMemManager.ptsPtr,5000000);
system("pause");
When I debug the program, the console window shows with a prompt "Press any key to continue." And then I close the window by clicking the close button. One out of ten times, the output window of visual studio shows the following:
Detected memory leaks!
Dumping objects ->
{83} normal block at 0x000001FC942D9070, 40000000 bytes long.
Data: < > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Object dump complete."
The program '[348] testForMemLeak.exe' has exited with code -1073741510 (0xc000013a).
I found it tricky to reproduce the result. Only if I clicked the close button right after the "busy" blue circle disappeared, should I get the memory leak message.
Is it because my memory handling function isn't good enough, or because the console application refused to call the destructor of memManager when I close the console window with bad timing?
Thanks.
User contributions licensed under CC BY-SA 3.0