Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 505
  • Last Modified:

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
0
ysgr
Asked:
ysgr
  • 5
  • 5
  • 2
  • +1
1 Solution
 
chensuCommented:
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
 
chackoCommented:
Put a 'return' statement after PostQuitMessage.
0
 
piano_boxerCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ysgrAuthor Commented:
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
 
piano_boxerCommented:
Where do you get the assertion?
0
 
ysgrAuthor Commented:
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
 
chensuCommented:
I think you should let users close the message box. Otherwise what is the message box for?
0
 
ysgrAuthor Commented:
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
 
chensuCommented:
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
 
ysgrAuthor Commented:
Hi Chensu,

that's what i am doing in OnTimer function.

ysgr
0
 
chensuCommented:
So, the WM_CLOSE should be sent only after the message box is closed. Then, where do you get the assertion failure?
0
 
ysgrAuthor Commented:
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
 
chensuCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now