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

x
?
Solved

Access violation when deleting CFrameWnd

Posted on 2006-11-02
9
Medium Priority
?
674 Views
Last Modified: 2013-11-20
Ah hello.

I have the following code in a handler for a button on a dialog:

void CDialogAppDlg::OnBnClickedButton1()
{
      if ( m_pFrameWnd )
      {
            m_pFrameWnd->DestroyWindow();
            delete m_pFrameWnd;
      }

      m_pFrameWnd = new CFrameWnd();
      m_pFrameWnd->Create ( NULL, _T("Frame Window") );
      m_pFrameWnd->UpdateWindow();
      m_pFrameWnd->ShowWindow ( SW_SHOW );

}

Assume

CFrameWnd* m_pFrameWnd;

Now, I can click the button once and create the window, but if I click it again I get access violation when calling

m_pFrameWnd->DestroyWindow()

Strangely enough, if I just create the window once, then close the app, I get no memory leaks, which I would expect from

      m_pFrameWnd = new CFrameWnd();

What is going on ?

TIA
0
Comment
Question by:mrwad99
[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
  • 5
  • 4
9 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17857559
CFrameWnd detetes itself in PostNCDestroy, it is enough to call DestroyWindow.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17857571
>> CFrameWnd detetes itself in PostNCDestroy

OK so that explains why I get no memory leaks

>> it is enough to call DestroyWindow.

I cannot call DestroyWindow, that is what crashes.  Or are you saying I don't need to ?

Thanks.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17857611
How exactly does it crash?
Try this:

m_pFrameWnd->SendMessage(WM_CLOSE, 0, 0);
0
Technology Partners: 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!

 
LVL 19

Author Comment

by:mrwad99
ID: 17857635
Thanks.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17857638
>> How exactly does it crash?

Not sure what you were getting at there.  Can you elaborate ?

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17857724
Code line, message etc. - as usual. But I think it is enough to remove delete m_pFrameWnd line.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17857825
>> But I think it is enough to remove delete m_pFrameWnd line.

I have stepped though the code and found that I don't even need the

m_pFrameWnd->DestroyWindow(), as this gets called when I close the window via the 'X' button.  But I would need to call this if the user closes the dialog before the main window, otherwise I would get leaks.

Interestingly so, when I do close the dialog with the frame window still active, the frame window does go away, and I get the leak on

m_pFrameWnd = new CFrameWnd();

detected.

I assume the OS destroys the CFrameWnd, but still, I only get one call into

CWnd::DestroyWindow()

which is called for the modal dialog.  Not for the CFrameWnd...

Do you know why this is ?

(Sorry to go on after I have closed this, but I read your last comment a bit late)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17857959
When you close dialog, application exits immediately. In this case possibly CFrameWnd is not deleted properly, because OnNCDestroy is not called, and there is memory leak. It is possible to debug this or read MFC code, but closing frame when dialog is closed fixes this problem in any case.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 17858057
Thanks again :o)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

660 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