mrwad99
asked on
Access violation when deleting CFrameWnd
Ah hello.
I have the following code in a handler for a button on a dialog:
void CDialogAppDlg::OnBnClicked Button1()
{
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
I have the following code in a handler for a button on a dialog:
void CDialogAppDlg::OnBnClicked
{
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How exactly does it crash?
Try this:
m_pFrameWnd->SendMessage(W M_CLOSE, 0, 0);
Try this:
m_pFrameWnd->SendMessage(W
ASKER
Thanks.
ASKER
>> How exactly does it crash?
Not sure what you were getting at there. Can you elaborate ?
Not sure what you were getting at there. Can you elaborate ?
Code line, message etc. - as usual. But I think it is enough to remove delete m_pFrameWnd line.
ASKER
>> 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)
I have stepped though the code and found that I don't even need the
m_pFrameWnd->DestroyWindow
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)
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.
ASKER
Thanks again :o)
ASKER
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.