Solved

Debug assertion while closing application

Posted on 1998-06-11
13
419 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
Comment Utility
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
Comment Utility
Put a 'return' statement after PostQuitMessage.
0
 
LVL 4

Expert Comment

by:piano_boxer
Comment Utility
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
Comment Utility
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
Comment Utility
Where do you get the assertion?
0
 

Author Comment

by:ysgr
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 23

Expert Comment

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

Author Comment

by:ysgr
Comment Utility
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
Comment Utility
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
Comment Utility
Hi Chensu,

that's what i am doing in OnTimer function.

ysgr
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This video discusses moving either the default database or any database to a new volume.

762 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

11 Experts available now in Live!

Get 1:1 Help Now