Link to home
Start Free TrialLog in
Avatar of ysgr
ysgr

asked on

Debug assertion while closing application

Hi,

I am developing a MDI application with Richeditview, tree view etc. I have a timer introduced in MainFrame class. In onTimer function, i am checking for certain conditions. If the checking condition is FALSE, i have to close the application and exit. so, i am calling PostMessage(WM_CLOSE) in OnTimer. This works fine, as long as there are no modal dialog box open at the time of destroying. Otherwise gives debug assertion. I tried with WM_QUIT, WM_DESTROY all results the same debug assertion.

Can anyone help to solve this debug assertion?

ysgr
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chacko
chacko

Put a 'return' statement after PostQuitMessage.
Another solution is to call this function from you timer handler when you want to close the application.

NOTE: If have to be inserted into your mainframe to work correctly!.

BOOL CMainFrame::ForceShutdown()
{
    // Get active window for this thread
    CWnd* pWnd = GetActiveWindow();

    // is this (mainwindow) enabled or active?
    if(IsWindowEnabled() || pWnd == this)
    {
        // Yes..., just self destruct
        PostMessage(WM_CLOSE);
        return TRUE;
    }

   
    // If I'm not enabled and the active windows is other
    // than my self, close the active window (could be a
    // dialog or messagebox).
    if(pWnd)
    {
        // Close the active popup window
        pWnd->SendMessage(WM_CLOSE);

        // Handle all pending messages
        MSG msg;
        while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        // Did we do it?
        if(GetActiveWindow() != pWnd)
        {
            // Yes.... self destruct
            PostMessage(WM_CLOSE);
            return TRUE;
        }
    }

    //
    // Oops, something we cant handle!
    //
    return FALSE;
}

-----------
ps: Reject prev answer if you think this
    is what you were asking for!
Avatar of ysgr

ASKER

Hi,

i get the debug assertion even if i have AfxMessageBox or MessageBox, how do i destroy them?

i tried piano-boxer's suggestion, does not solve my problem.

ysgr
Where do you get the assertion?
Avatar of ysgr

ASKER

Assertion is in winctrl4.cpp at line 108

void CRichEditCtrl::SetSel(long nStartChar, long nEndChar)
{
      ASSERT(::IsWindow(m_hWnd));  //( failing in this line);
      CHARRANGE cr;
      cr.cpMin = nStartChar;
      cr.cpMax = nEndChar;
      ::SendMessage(m_hWnd, EM_EXSETSEL, 0, (LPARAM)&cr);
}

I think you should let users close the message box. Otherwise what is the message box for?
Avatar of ysgr

ASKER

Yes, I can allow users to close the message box. but if any AfxMessageBox is open, it gives this debug assertion. Message BOx  takes the user input and finally closes the application.  When the checking condition fails in Timer function , i am displaying a AfxMessageBox. does it has anything to do with asserton.

ysgr
The following code should work in OnTimer. You should kill the timer first, pop up the message box and post the WM_CLOSE message finally.

this->KillTimer(...);
::AfxMessageBox(..);
this->PostMessage(WM_CLOSE);

Avatar of ysgr

ASKER

Hi Chensu,

that's what i am doing in OnTimer function.

ysgr
So, the WM_CLOSE should be sent only after the message box is closed. Then, where do you get the assertion failure?
Avatar of ysgr

ASKER

Hi Chensu,

i am not getting the assertion for ontimer message box.  i have some other message boxes in the application. if that is open and before closing that box, if ontimer function is called ( and  if i am forcing the application to close)i am getting the assertion.

ysgr
I see. So, you should set a flag indicating the message box is active. If the message box is there, you should wait until it is closed by the users.