Memory leak detection when using heapalloc and heapfree

If your developing a product which is called a thousand many times and runs in the background as a process, even a byte of memory might be very important to you. Consider a application which is scheduled to run every few seconds for checking a update. This might leak a byte every time its called, so detecting such leaks will be very necessary.

You can make use of heapfree and heapsize to fix this leaks once you allocate memory.
Use heapalloc to allocate memory:
retValue = (RASENTRYNAME *) HeapAlloc(GetProcessHeap(), 0, sizeAlloc);
typeI32 mySize = HeapSize(GetProcessHeap(),0, <>);
std::wstring str = CStringUtils::I32ToStr(mySize);
std::string myStr = CStringUtils::WStringToString(str);
Write the memory size to a file.
FILE * pFile;
pFile = fopen (c:myfile.txt,a);
const char *p;
p=myStr.c_str();
if (pFile!=NULL)
{
fputs (p,pFile);
fclose (pFile);
}
From the above code you will know how much memory has been used.
You can free the memory at the end using heapfree and then again call this code. you should see zero memory usage.
HeapFree(GetProcessHeap(), 0, pointer to memory you initialize);
pointer to memory you initialized= NULL;

In

Leave a Reply

Your email address will not be published. Required fields are marked *