Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Simple question about CLists

Posted on 2003-03-30
2
Medium Priority
?
271 Views
Last Modified: 2013-11-20
How can I prevent memory leaks in a list of pointers when objects are created and added like this:

void CMyClass::MyFunction()
{
  CMyMsg *pMsg = new CMyMsg;
  ASSERT(pMsg);
  //work with pMsg
  .....
  m_MsgList.Addtail(pMsg);
}
where m_MsgList is a member of CMyClass and is defined like this:

CList<CMyMsg *, CMyMsg*>m_MsgList;

If I write delete pMsg; at the of the function the information in m_MsgList is also deleted. If I don't delete pMsg obviously memory leaks appear... Please help...
0
Comment
Question by:lbalan791
2 Comments
 
LVL 4

Expert Comment

by:bistrica
ID: 8234591
delete each pointer first, when clearing

m_MsgList.Addtail

like:

int size = m_MsgList.getsize();
for (int i =0;i<size;i++)
{
   delete m_MsgList[i];
}

m_MsgList.removeall();
0
 
LVL 12

Accepted Solution

by:
williamcampbell earned 300 total points
ID: 8241573


make a smart pointer class

#include <afxtempl.h>

class CMyList : public CList<CMyMsg*, CMyMsg*>
{
public:
     CMyList () {}
     ~CMyList ()
     {
          POSITION pos = GetHeadPosition();
          for (int i=0;i < GetCount();i++)
          {
             delete (CMyMsg*)GetNext(pos);
          }
     }
};

CMyList ml;

void MyFunction ()
{
     CMyMsg* msg = new CMyMsg;
     ml.AddTail ( msg );
}


when ml goes out of scope then all the memory will be freed.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question