|
Question : Memory Leak using CObArray
|
|
I'm trying to implement a CObArray of a class I derived from CObject.
class CMyClass : public CObject { private: CString m_name; CString m_grade; CString m_credits;
public: // Constructors CMyClass(); CMyClass(CString, CString, CString); CMyClass(const CStorGrades&); // Destructor ~CMyClass();
// Accessors void SetName(CString); CString& GetName(); void SetGrade(CString); CString& GetGrade();
void SetCredits(CString); CString& GetCredits(); };
I declare the CObArray :
CObArray m_MyClasses;
And try to initialize it:
CMyProgDoc *pDoc=GetDocument();
pDoc->m_MyClasses.SetSize(5);
for (int i=0; i<5; i++) { pDoc->MyClasses.SetAt(i, new MyClass); }
At runtime, a memory leak is asserted at this point.
I'm using MSVC 4.0. Does this look wrong to anyone?
-- please respond via email Thanks, Corey
|
|
Answer : Memory Leak using CObArray
|
|
As i think, you doing this in CView derived class: class CMyView: public CView { [...] CObArray m_MyClasses; };
When you have to write: CMyView::~CMyView () { for ( int i = 0 ; i < m_MyClasses.GetCount() ; i ++ ) { delete m_MyClasses.[i]; } m_MyClasses.RemoveAll(); }
Because CObList and CObArray containing only pointers on objects and dont delete objects in destructor. You have to delete objects yourself.
|
|
|
|