Heap object created on the stack isn't cleaned up properly
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Heap object created on the stack isn't cleaned up properly

  1. #1
    Join Date
    Dec 2010
    Posts
    601

    Heap object created on the stack isn't cleaned up properly

    Code:
    m_vRenderObjects.push_back(new Objects(mOperatorMesh));
    
    /// this is never called
    ~Objects(void) { 
    		OutputDebugStringA("Cleanup Objects\n");
    		if (StateMachine != NULL)
    		{
    			delete StateMachine; 
    			StateMachine = NULL;
    		}
    
    		if (m_pAnimCtrl != NULL)
    		{
    			m_pAnimCtrl->Release(); 
    			m_pAnimCtrl = NULL;
    		}
    	
    		dtFreeNavMeshQuery(m_navQuery); 
    		m_navQuery = NULL;
    	}
    This leads to ugly memory leaks.
    Help please.
    Thanks
    Jack

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,273

    Re: Heap object created on the stack isn't cleaned up properly

    You could use a std::shared_ptr<Objects> or perhaps std::unique_ptr<Objects> instead, or use a container like boost:tr_vector. Otherwise, you need to delete what you new.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Heap object created on the stack isn't cleaned up properly

    Quote Originally Posted by lucky6969b View Post
    [code]
    m_vRenderObjects.push_back(new Objects(mOperatorMesh));

    /// this is never called
    ~Objects(void)
    Unless your program just aborts, destructors are always called.
    Code:
    if (StateMachine != NULL)
    An unnecessary test. There is no need to test for NULL if you're calling delete. Calling delete on a NULL pointer results in no operation being performed.
    Heap Object created on the stack...
    It doesn't matter where the heap object is created (whatever that means). If you call "new", then it's your responsibility to call delete, unless you wrap the call to "new" in a smart pointer, as laserlight pointed out. Getting involved in whether a heap object is created here or there misses the point.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 21st, 2013 at 04:30 AM.

  4. #4
    Join Date
    Mar 2006
    Posts
    139

    Re: Heap object created on the stack isn't cleaned up properly

    From Paul McKenzie:
    An unnecessary test. There is no need to test for NULL if you're calling delete.
    Quite true! What fools some programmers into thinking otherwise is that the C Run Time will sometimes check the integrity of the heap in a debug build on calls to delete (or at least it used to in earlier versions of Visual Studio). If the heap is corrupted those versions put up an error box and halted the program.

    I worked with a programmer once who insisted deleting a NULL pointer wasn't generally safe, but it was because this was happening to him at a point where he was calling delete on a pointer that happened to be NULL. It seems people assume if they get the detection on deletion of a non-NULL pointer, something is wrong with their program, but they won't reach that same conclusion if they are deleting NULL.

    Apparently there are quite a few people out there who've been conditioned to wrap their deletes, perhaps because of this. (?)

  5. #5
    Join Date
    Jun 2012
    Posts
    58

    Re: Heap object created on the stack isn't cleaned up properly

    I used to wrap if(p != 0) around my deletes for some time.
    The reason was, as you mentioned above, that older VS runtimes would popup an error when deleting NULL.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center