Solved

Memory leak

Posted on 2004-03-25
3
466 Views
Last Modified: 2012-06-22
The program I inherited gives me constant memory problems,
here's the full code:

CPtrArray m_AccuStackItems;
CAccuStackItem is a very simple class with just a few properties.

CAccuStackReader::~CAccuStackReader()
{

      for(int i =0; i< m_AccuStackItems.GetSize(); i++)
      {
            delete m_AccuStackItems[i];
      }
      m_AccuStackItems.RemoveAll();

}

bool CAccuStackReader::LoadAccuStackItems(CString FileName)
{
      CStdioFile F;
      CString S;
      CString T;
      int Pos;
      
      if(F.Open(FileName,CFile::modeRead) )
      {
            while ( F.ReadString(S) )
            {
                  CAccuStackItem* pasi = new CAccuStackItem;
                  T = S;
                  Pos = 0;
                  Pos = T.Find(",");
                  pasi->SetTrack(T.Left(Pos));
                  Pos++;
                  T = T.Mid(Pos);
                  Pos = T.Find(",");
                  pasi->SetEndPoint(atol(T.Left(Pos)));
                  Pos++;
                  T = T.Mid(Pos);
                  Pos = T.Find(",");
                  pasi->SetRow(T.Left(Pos));
                  Pos++;
                  T = T.Mid(Pos);
                  pasi->SetLot(T);
                  m_AccuStackItems.Add(pasi);
            }
            F.Close();
            return true;
      }
      return false;
}
0
Comment
Question by:jd9288
[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
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10680816
What exactly do you mean by 'constant memory problems' and what is the declaration of 'CAccuStackItem'? The above seem OK at the 1st glance...

BTW, I assume the problem is using a 'CPtrArray' - it only stores 'void*', so 'delete' will not call a destructor. Try

CAccuStackItem* p = m_AccuStackItems[i];
delete p;

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 10680873
Ooops, that should of course read

CAccuStackItem* p = (CAccuStackItem*) m_AccuStackItems[i];
delete p;
0
 

Author Comment

by:jd9288
ID: 10680941
Brilliant! Thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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