Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with OnClose causing a crash w/ CDocument

241 views
Skip to first unread message

Josh McFarlane

unread,
Oct 3, 2005, 3:33:32 PM10/3/05
to
I'm trying to shut down my application from an SDI frame.

In CFrameWnd, the program crashes as stated below:

if (pDocument != NULL && pDocument->m_bAutoDelete)
{
BOOL bOtherFrame = FALSE;
POSITION pos = pDocument->GetFirstViewPosition();
while (pos != NULL)
{
CView* pView = pDocument->GetNextView(pos); //dies at this point
ENSURE_VALID(pView);
if (pView->GetParentFrame() != this)
{
bOtherFrame = TRUE;
break;
}
}
if (!bOtherFrame)
{
pDocument->OnCloseDocument();
return;
}

// allow the document to cleanup before the window is destroyed
pDocument->PreCloseFrame(this);
}

// then destroy the window
DestroyWindow();

So, I thought maybe try to set autodelete to false and then delete it
afterwards. Unfortunately, now it dies at DestroyWindow. Anyone have
any idea why it's crashing like this?

Thanks,
Josh McFarlane

David Lowndes

unread,
Oct 3, 2005, 4:42:02 PM10/3/05
to
>I'm trying to shut down my application from an SDI frame.

How, what are you doing?

>In CFrameWnd, the program crashes as stated below:

Is this MFC code or your code?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Josh McFarlane

unread,
Oct 3, 2005, 11:01:19 PM10/3/05
to
David Lowndes wrote:
> How, what are you doing?

I've overwritten the CMainFrame::OnClose to throw a final shutdown to
my alternate threads, then post another WM_CLOSE message. On the 2nd
pass through it calls the parent function CFrameWnd::OnClose(), which
crashes as specified above.

> Is this MFC code or your code?

The above posted code is MFC code.

I've tried numerous things, such as setting autodelete on the document
to false, and then deleting it after CFrameWnd::OnClose, which causes a
crash on the DestroyWindow instead of MFC. I've also tried deleting the
document before hand, which cause OnClose to complete successfully, but
then returns to the message pump rather than killing the App.

How should I close the application from CMainFrame::OnClose?

David Lowndes

unread,
Oct 4, 2005, 2:26:45 AM10/4/05
to
>I've overwritten the CMainFrame::OnClose to throw a final shutdown to
>my alternate threads, then post another WM_CLOSE message. On the 2nd
>pass through it calls the parent function CFrameWnd::OnClose(), which
>crashes as specified above.

Doing this is most likely the root cause of the problem.

Presumably you want to synchronise the close down of worker threads in
your application at this point - if so, why don't you just have your
OnClose handler wait until they've finished rather than create another
WM_CLOSE message?

Josh McFarlane

unread,
Oct 4, 2005, 11:53:16 AM10/4/05
to

David Lowndes wrote:
> Doing this is most likely the root cause of the problem.
>
> Presumably you want to synchronise the close down of worker threads in
> your application at this point - if so, why don't you just have your
> OnClose handler wait until they've finished rather than create another
> WM_CLOSE message?

The worker threads while shutting down post messages to the parent
thread with data that needs to be dealt with. After they shutdown
successfully, I post the WM_CLOSE to ensure that any prior thread
messages are handled before the actual shutdown. I'll post the code
this afternoon.

Josh

Josh McFarlane

unread,
Oct 4, 2005, 3:24:54 PM10/4/05
to
David Lowndes wrote:
> Doing this is most likely the root cause of the problem.
>
> Presumably you want to synchronise the close down of worker threads in
> your application at this point - if so, why don't you just have your
> OnClose handler wait until they've finished rather than create another
> WM_CLOSE message?

I just tried the basic troubleshooting step I forgot to do: Using
default WM_CLOSE handler. Same error message, same problem. I think
this may have something to do with the fact that I never really use the
Doc, only the View. I leave the default document open and just draw
images from the inputs on the screen.

Is there a special function I'm suppose to call on the Document or View
before calling WM_CLOSE?

David Lowndes

unread,
Oct 5, 2005, 4:45:55 AM10/5/05
to
>I just tried the basic troubleshooting step I forgot to do: Using
>default WM_CLOSE handler. Same error message, same problem. I think
>this may have something to do with the fact that I never really use the
>Doc, only the View. I leave the default document open and just draw
>images from the inputs on the screen.

If you just do nothing with the document, the default operation should
work. However, if you mean that you do something to subvert the
default behaviour - that may be another kettle of fish.

The best suggestion I can make at this stage is to re-check your ideas
in a clean project to eliminate anything you've done in your real
project.

Josh McFarlane

unread,
Oct 6, 2005, 12:42:29 AM10/6/05
to
David Lowndes wrote:
> If you just do nothing with the document, the default operation should
> work. However, if you mean that you do something to subvert the
> default behaviour - that may be another kettle of fish.

See, that's the weird thing. The only real doc / view action I ever do
is overriding the OnDraw for the View, which draws the image data that
is coming in via the other threads. In theory this should do nothing,
as there is no new / open / close doc menu items.

David Lowndes

unread,
Oct 6, 2005, 2:35:01 AM10/6/05
to

Try prototyping a simple version of your closedown operations in a
clean project and let us know how to reproduce the behaviour you're
getting.

0 new messages