• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 991
  • Last Modified:

InitInstance code placement (mysterious app crash)

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
meghani
Asked:
meghani
1 Solution
 
SalteCommented:
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
 
jkrCommented:
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
 
meghaniAuthor Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
udilCommented:
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
 
SalteCommented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now