Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Memory leak

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

721 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