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

x
?
Solved

?Memory Leak? Using CObArray

Posted on 1997-10-14
3
Medium Priority
?
293 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
[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
 

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 100 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

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