Solved

Memory leak

Posted on 2004-03-25
3
460 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
Comment Utility
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
Comment Utility
Ooops, that should of course read

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

Author Comment

by:jd9288
Comment Utility
Brilliant! Thank you!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now