?
Solved

Simple question about CLists

Posted on 2003-03-30
2
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

764 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