Solved

?Memory Leak? Using CObArray

Posted on 1997-10-14
3
287 Views
Last Modified: 2013-11-20
I have a class:
    class CMyClass : public CObject
     {
      private:
       CString m_name;
       CString m_credits;
       CString m_grade;
      public:
       //constructors/destructor
       //accessor functions
 };

I declare a CObArray in the CMyProjDoc class:
   CObArray m_courses;

And initialize it in the CMyProjView class:

CMyProjView::CMyProjView()
{
    CGradesDoc *pDoc=GetDocument();

    pDoc->m_courses.SetSize(5);
    for(int i=0; i<5; i++)
    {
      pDoc->m_courses.SetAt(i, new(CStorGrades));
    }
}

and destruct it: (as per advice from jaba)

CMyProjView::~CMyProjView()
{
   CGradesDoc *pDoc=GetDocument();

   for ( int i=0; i<pDoc->m_courses.GetSize(); i++)
   {
     delete pDoc->m_courses[i];
   }
pDoc->m_courses.RemoveAll();
}

The problem arises in a debug assertion at:
                           
line 44
file: objcore.cpp (MFC source file)
in routine: CObject::IsKindOf(...)
on the statement: ASSERT(this != NULL)

The documentation on CObject says that the overloaded new operator:

 "In the Debug version, operator new participates in an allocation-monitoring scheme designed to detect memory leaks."

And the debugger reported that:

Loaded symbols for 'C:\WINDOWS\SYSTEM\MFC40D.DLL'
Loaded symbols for 'C:\WINDOWS\SYSTEM\MSVCR40D.DLL'
Memory Leak detected...etc

I'm pretty sure the ASSERT fails at the new.

Jaba: I tried changing the destructor to:

CMyProjDoc* pDoc;
pDoc->DeleteContents();

but the ASSERT still fails.

Am I to assume that the CView constructor is the wrong place for initializing the CObArray? What would be a generic example for the use of a CObject derived class and a CObArray? That's probably the best place for me to start.

-- Corey
0
Comment
Question by:sanderc2
  • 2
3 Comments
 

Author Comment

by:sanderc2
ID: 1308139
I moved the initialization to the Document class and I stopped getting the assertion fail. Why is this?

-- Corey

p.s. now I have another CObArray assertion fail
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1308140
OK - found it now - thanks.

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1308141
Firtly, the destruction should be in the document class - the class that owns the data.  Doing it in the view is wrong - especially if you go MDI, because there is more than one view.

Also the document pointer in the view has not been set up in the constructor, sopDoc would probably be NULL.  If you want to do that sort of access that need the document pointer, do it in OnInitialUpdate.

Also you have derived from CObject, but you have not done any DECLARE_SERIAL/IMPLEMENT_SERIAL (or DYNAMIC or DYNCREATE).  This means the run-time info won't be setup.

First all these things and if you still have any problems, let me know.

mailto:Roger_Onslow@compsys.com.au

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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