Discussion:
Memory Leak with CMemFile
(too old to reply)
Anthony Wieser
2005-04-21 11:46:43 UTC
Permalink
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
Mihajlo Cvetanovic
2005-04-21 13:21:37 UTC
Permalink
Post by Anthony Wieser
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.
Using some common sense I'd say that Detach should set m_bAutoDelete to
FALSE, and Attach shouldn't set it to TRUE.
no stress
2005-04-21 15:16:28 UTC
Permalink
Been there, experienced that.
Post by Anthony Wieser
After quite awhile tracking down a memory leak this morning, I came across
one that doesn't seem to be documented anywhere.
{
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
no stress
2005-04-21 15:18:02 UTC
Permalink
I think I created a derived class to just retrieve the ptr.
Post by Anthony Wieser
After quite awhile tracking down a memory leak this morning, I came across
one that doesn't seem to be documented anywhere.
{
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
u***@yahoo.com
2016-07-08 17:58:58 UTC
Permalink
Seems like Attach should have option to restore bAutoDelete.

CMemFile::Attach(BYTE* pbuf, int len, BOOL bAutoDelete=FALSE);

This way, if you just detach and re-attach, you can force bAutoDelete to TRUE, to *really* restore the memfile to how it was (so the destructor free the memory). Oversight by MS to not provide this option.

As it is, you have to free the memory yourself: e.g. free( MF.Detach() )
Loading...