Re: memory leak
From: Alexander Grigoriev (alegr_at_earthlink.net)
Date: 08/14/04
- Next message: Jeff Partch [MVP]: "Re: dumb dos question"
- Previous message: daniel kaplan: "dumb dos question"
- In reply to: RuffAroundTheEdges: "Re: memory leak"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 14 Aug 2004 08:01:35 -0700
You're NOT supposed to 'delete' COM objects. Your code doesn't crash just
because you zero the pointer before deleting it.
Last Release() call applied to a COM object is what actually does 'delete',
whatever it means for that particular object.
In other cases, you should 'delete' object BEFORE zeroing a pointer.
"RuffAroundTheEdges" <RuffAroundTheEdges@discussions.microsoft.com> wrote in
message news:DF3844DE-385C-43F6-86CE-F17A6287151D@microsoft.com...
> Can anybody explain the following:
> My test program is a win32 application. I have the following method to
clean
> up pointers:
>
> EXAMPLE 1
> void CleanUp()
> {
> if (m_pIControl != NULL)
> {
> m_pIControl->CleanTemp();
> m_pIControl->CleanUp();
> m_pIControl->Release();
> m_pIControl = NULL;
> delete m_pIControl;
> }
>
> if (m_pIDraw != NULL)
> {
> m_pIDraw->Release();
> m_pIDraw = NULL;
> delete m_pIDraw;
> }
>
> if (m_pIUnknown != NULL)
> {
> m_pIUnknown->Release();
> m_pIUnknown = NULL;
> delete m_pIUnknown;
> }
>
> OleUninitialize();
> }
>
> My dll is a staic link MFC type.
> I followed the code example from the MSDN Library's technical articles.
> MFC/COM Objects 1: Creating a Simple Object
> Nigel Thompson
> Microsoft Developer Network Technology Group
>
> March 6, 1995
>
> from the Component Object Model sub-section.
>
> The m_pIControl->CleanTemp() is in the dll:
>
> STDMETHODIMP CDrawControl::XControl::CleanTemp()
> {
> METHOD_PROLOGUE(CDrawControl, Control);
> pThis->CleanTemp();
> return NOERROR;
> }
>
> void CDrawControl::CleanTemp()
> {
> CWinApp* pApp = AfxGetApp();
> long count = 0;
> count = (long)pApp->OnIdle(count);
> while (count != 0)
> count = (long)pApp->OnIdle(count);
> }
>
> This process came from the article:
> HOWTO: Clean Up Temporary MFC Object in _USRDLL DLLs
> Last reviewed: July 31, 1997
> Article ID: Q105286
>
>
> m_pIControl->CleanUp() is in the dll:
>
> STDMETHODIMP CDrawControl::XControl::CleanUp()
> {
> METHOD_PROLOGUE(CDrawControl, Control);
> pThis->CleanUp();
> return NOERROR;
> }
>
> void CDrawControl::CleanUp()
> {
> if (m_p8BitClass)
> {
> delete m_p8BitClass;
> m_p8BitClass = NULL;
> }
> }
>
> Calling EXAMPLE'S 1 CleanUp method gets rid of the memory leak. However,
if
> I change the CleanUp() method to the following:
>
> EXAMPLE 2
>
> void CleanUp()
> {
> if (m_pIControl != NULL)
> {
> m_pIControl->CleanTemp();
> m_pIControl->CleanUp();
> m_pIControl->Release();
> m_pIControl = NULL;
> delete m_pIControl;
> }
>
> if (m_pIDraw != NULL)
> {
> m_pIDraw->Release();
> m_pIDraw = NULL;
> delete m_pIDraw;
> }
>
> if (m_pIUnknown != NULL)
> {
> m_pIUnknown->Release();
>
> ////////////////CHANGED HERE/////////////////////////////
> // deleted pointer first then set it to NULL.
> ////
> delete m_pIUnknown;
> m_pIUnknown = NULL;
> }
>
> OleUninitialize();
> }
>
> I get the following:
> Debug Assertion Failed.
> Expression: _BLOCK_TYPE_IS_VALID(pHeader->nBlockUsed)
> ...
>
> Why is it that this method in the dll:
> void CDrawControl::CleanUp()
> {
> if (m_p8BitClass)
> {
> delete m_p8BitClass;
> m_p8BitClass = NULL;
> }
> }
>
> does not throw the same debug assertion failure?
>
>
> "David Webber" wrote:
>
> >
> > "RuffAroundTheEdges" <RuffAroundTheEdges@discussions.microsoft.com>
> > wrote in message
> > news:F97ACA8D-AEB9-4905-B7CF-B6F0A88ED1EC@microsoft.com...
> >
> > > I have this MFC static link dll that throws the following memory
> > leak.
> >
> > DLL stands for *Dynamic* link library - the opposite of "static"
> > link.
> >
> > > The code that creates this havoc is CBitClass m_pBitClass = new
> > CBitClass();
> > > I do the usual m_pBitClass = NULL; delete m_pBitClass;
> >
> > That is *not* usual. Setting the pointer to NULL before using the
> > delete operator will guarantee that the delete operator does nothing
> > and will thus guarantee a memory leak.
> >
> > Try
> >
> > delete m_pBitClass; // Deletes what m_pBitClass points to
> > m_pBitClass = NULL; // Prevents you accidentally trying to
> > access what you have just deleted.
> >
> > Dave
> > --
> > David Webber
> > Author MOZART the music processor for Windows -
> > http://www.mozart.co.uk
> > For discussion/support see
> > http://www.mozart.co.uk/mzusers/mailinglist.htm
> >
> >
> >
- Next message: Jeff Partch [MVP]: "Re: dumb dos question"
- Previous message: daniel kaplan: "dumb dos question"
- In reply to: RuffAroundTheEdges: "Re: memory leak"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|