?
Solved

InitInstance code placement (mysterious app crash)

Posted on 2003-02-20
7
Medium Priority
?
945 Views
Last Modified: 2007-12-19
I want to do some verification in my InitInstance() function after the main window is displayed. So I did the following:

m_pMainWnd = new CMainFrame();

if(!CheckSomething())
     return false;

Now, I find that the app crashes every time I try to close it (and that too after a long time and goes into NTDLL). Debug Monitor shows me a warning

[1136] Warning: Destroying non-NULL m_pMainWnd

Now, if I place the checking code before i initialize the m_pMainWnd, everything remains Ok.

Whats happening?
0
Comment
Question by:meghani
[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
7 Comments
 
LVL 12

Expert Comment

by:Salte
ID: 7987330
Normally the m_pMainWnd pointer isn't initialized by user code. It is normally initialized in MFC's framework. Is it possible you are trying to set that pointer to some value without doing it through the MFC as you are supposed to? If so MFC might well - when it is initializing stuff - figuring out that the pointer value is garbage and attempt to destroy the window or other object stored there.

It is hard to pin point what is going on, I assume that CheckSomething doesn't attempt to modify any data anywhere (const function), but it might call functions with side effects.

Would be nice if you could show us CheckSomething() especially what MFC functions - if any - that it calls and if it modifies any member variables anywhere etc...

Alf
0
 
LVL 86

Expert Comment

by:jkr
ID: 7987372
This message comes from

    // Perform specific initializations
    if (!pThread->InitInstance())
    {
        if (pThread->m_pMainWnd != NULL)
        {
            TRACE0("Warning: Destroying non-NULL m_pMainWnd\n");
            pThread->m_pMainWnd->DestroyWindow();
        }
        nReturnCode = pThread->ExitInstance();
        goto InitFailure;
    }
    nReturnCode = pThread->Run();

(winmain.cpp)

The rule of thumb is - if you return 'FALSE' from 'InitInstance()', don't create a window beforehand.
0
 

Accepted Solution

by:
meghani earned 0 total points
ID: 7991744
Well I solved the problem:

if(!CheckSomething()) {
   m_pMainWnd->PostMessage(WM_CLOSE);
   delete m_pMainWnd;
   m_pMainWnd = NULL;
   return FALSE;
}

In my case it has solved the problem... I think posting WM_CLOSE just before deleting may fail sometimes, cause it may not be able to call DestroyWindow().
0
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!

 
LVL 2

Expert Comment

by:udil
ID: 8868883
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.

In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Udil
EE Cleanup Volunteer
0
 
LVL 12

Expert Comment

by:Salte
ID: 8869716
Hmm... you should never call delete m_pMainWnd after calling PostMessage(WM_CLOSE).

The reason is that the WM_CLOSE message hasn't been processed yet when PostMessage returns.

The correct way is to call PostMessage(WM_CLOSE) and then have an OnClose() function which can delete the pointer if you want to delete the object. In that case you wait until WM_CLOSE has actually been processed before you delete the object.

Alf
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 9355960
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Delete question without a refund.  He found the solution himself but failed to close the question.

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Ged
EE Cleanup Volunteer

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 This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

771 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