CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 21

Threaded View

  1. #1
    Join Date
    May 2004
    Posts
    474

    Memory leak - arghh!

    This code creates a memory leak:

    Code:
    CShape *shape = new CShape();
    	shape->m_sName = newShape.m_sShapeName;
    	shape->m_sPoints = newShape.m_sShapePoints;
    	shape->m_nSides = newShape.m_nSides;
    	shapes->Add(shape);
    	CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
    	pFrame->m_TreeView->AddShapeToTree(shape);
    If I delete shape after this code then it messes up my shapes array (CArray <CShape *, CShape *>). I do delete the shapes array when the program closes.

    What am I doing wrong?

    Bit more info. Here's where I create the array:

    Code:
    shapes = new CArray<CShape *, CShape *>();
    And here's deleting it:

    Code:
    for (int x = 0; x < theApp.shapes->GetCount(); x++)
    		delete theApp.shapes->GetAt(x);
    
    	theApp.shapes->RemoveAll();
    I am missing something obvious. Why do I still get a memory leak?
    Last edited by richiebabes; March 15th, 2009 at 10:08 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured