Solved

?Memory Leak? Using CObArray

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 110
VBA "SendKeys" Syntax for Multiple Keystrokes 7 98
countEvens challenge 2 59
noX challenge 17 77
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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 remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now