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
Solved

?Memory Leak? Using CObArray

Posted on 1997-10-14
3
288 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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