Re: memory leak

From: Alexander Grigoriev (alegr_at_earthlink.net)
Date: 08/14/04


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
> >
> >
> >



Relevant Pages

  • Re: realloc in function
    ... My note about memory leak is also very important for proper use of realloc. ... > Keep in mind that assigning realloc result to the same pointer may lead ... > Why would you read Microsoft news from anywhere other then the MS news ... > " MVPs earn their status by being nominated by peers and Microsoft ...
    (microsoft.public.vc.language)
  • Re: standard memory allocator alignment issue...
    ... One thing I might do is write my own memory allocator. ... once to get a big chunk of memory, ... guarantee that the pointers returned by my_malloc are properly ... can't see a way to adjust a pointer using it in any potable way. ...
    (comp.lang.c)
  • Re: Boolean Buyers Beware ... AIX compiler bug --- PMR 26241,756
    ... The quintessential example of what I'm talking about is the pointer you get back from 'malloc'. ... You've made a whole bunch of assertions on this thread with no ... where you get to size your buckets. ... Just use Google Code search for "guarantee", ...
    (comp.programming.threads)
  • Re: CList Concept
    ... pointer instead of a list of object. ... called automatically by the list class destructor. ... CObject myObject = new CObject; ... if I do it this way, will I get memory leak? ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: Advanced pointer typecasting
    ... One guarantee that the language makes is that a pointer to any type of ... originally points to a valid object of its type, the char or void ... There is no such guarantee for any other type of pointer, ... There can be padding in between members, ...
    (comp.lang.cpp)

Loading