Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Memory leak

Posted on 2004-03-25
3
Medium Priority
?
474 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
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

885 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