Solved

?Memory Leak? Using CObArray

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Different colored text in ComboBox without Subclassing 8 76
Expand to include initial dialog with two choices. 9 94
substring method in java 1 142
Problem to App source 6 120
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. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 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