Solved

Debug assertion while closing application

Posted on 1998-06-11
13
436 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:ysgr
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1317878
You should set a flag indicating a modal dialog box is active, in which case do not PostMessage(WM_CLOSE). Because a modal dialog box is supposed to be closed by using EndDialog. You may get a pointer to the dialog box object and call EndDialog.
0
 
LVL 1

Expert Comment

by:chacko
ID: 1317879
Put a 'return' statement after PostQuitMessage.
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1317880
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!
0
 

Author Comment

by:ysgr
ID: 1317881
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
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1317882
Where do you get the assertion?
0
 

Author Comment

by:ysgr
ID: 1317883
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);
}

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:chensu
ID: 1317884
I think you should let users close the message box. Otherwise what is the message box for?
0
 

Author Comment

by:ysgr
ID: 1317885
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
0
 
LVL 23

Expert Comment

by:chensu
ID: 1317886
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);

0
 

Author Comment

by:ysgr
ID: 1317887
Hi Chensu,

that's what i am doing in OnTimer function.

ysgr
0
 
LVL 23

Expert Comment

by:chensu
ID: 1317888
So, the WM_CLOSE should be sent only after the message box is closed. Then, where do you get the assertion failure?
0
 

Author Comment

by:ysgr
ID: 1317889
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
0
 
LVL 23

Expert Comment

by:chensu
ID: 1317890
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 106
Unix / Linux grid computing 5 149
Add content to output file 4 62
List out all word 7 261
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

862 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now