Solved

Debug assertion while closing application

Posted on 1998-06-11
13
482 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
[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
  • 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
Technology Partners: 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!

 

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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 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