Solved

Memory leak

Posted on 2004-03-25
3
468 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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