Anthony Wieser
2005-04-21 11:46:43 UTC
After quite awhile tracking down a memory leak this morning, I came across
one that doesn't seem to be documented anywhere.
The offending code looked like this:
{
CMemFile mf;
...
BYTE *pBuf = memFile.Detach();
// do something with the buffer
memFile.Attach(pBuf, len);
}
While you might think that Attach puts things back to how they used to be,
it in fact changes the m_bAutoDelete protected member of CMemFile to be
FALSE, and therefore doesn't clean up the memory.
So, you'll need to do this instead to avoid the leak....
{
CMemFile mf;
...
BYTE *pBuf = memFile.Detach();
// do something with the buffer
delete pBuf;
}
--
Anthony Wieser
Wieser Software Ltd
Analyze your weblogs with TopDrop
www.wieser-software.com/topdrop/?050421
one that doesn't seem to be documented anywhere.
The offending code looked like this:
{
CMemFile mf;
...
BYTE *pBuf = memFile.Detach();
// do something with the buffer
memFile.Attach(pBuf, len);
}
While you might think that Attach puts things back to how they used to be,
it in fact changes the m_bAutoDelete protected member of CMemFile to be
FALSE, and therefore doesn't clean up the memory.
So, you'll need to do this instead to avoid the leak....
{
CMemFile mf;
...
BYTE *pBuf = memFile.Detach();
// do something with the buffer
delete pBuf;
}
--
Anthony Wieser
Wieser Software Ltd
Analyze your weblogs with TopDrop
www.wieser-software.com/topdrop/?050421