[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

debug arssertion failed

Posted on 2008-02-05
8
Medium Priority
?
423 Views
Last Modified: 2013-12-14
Hi,
I have a project in vc++ 6.0.
I am reading sector by sector of a volume  using Createfile() SDK function  & show informations about the process through a progressbar(CProgressCtrl) on a  dialog.
I am using SendMessage() function to send informations to the ProgressCtrl from my .cpp file to the  dialog's ProgressCtrl . But in betwen process I want to stop the process, so I just close dialog. But at that time send message is trying to send Message to the ProgresCtrl, which is not exist(becose i have already close the dialog). hence the debug assertion failed error comes.

My question is, how to solve the problem. when user press stop button I have to stop the process & close the dialog.

If I use Sleep() function before close the dialog , then it is OK, but i don't want to  use Sleep function.
pls tell me any other way to solve the problem.

what exactly happened, that
The" m_hWnd" variable is NULL.

here is the error..

_AFXWIN_INLINE BOOL CWnd::PostMessage(UINT message, WPARAM wParam, LPARAM lParam)
      { ASSERT(::IsWindow(m_hWnd)); return ::PostMessage(m_hWnd, message, wParam, lParam); }
 
0
Comment
Question by:davinder101
  • 4
  • 3
8 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 20830190
Beofre you post the message make a check
if(IsWindow(hWnd))  PostMessage(....)
0
 

Author Comment

by:davinder101
ID: 20831673
Hi AndyAinscow,
Before Postmessage(), hWnd is a valid handle in my program.
So the above solution given by you is failed .(I have already tried before)
I mean one or more  messages(dispatched by PostMessage() function ) are already in the message queue and waiting to be process, At this time I want to close the dialog, So my dialog is closed but windows send the message(dispatched by PostMessage() and waiting for process from message queue) to the dialog.

So ask for any other way to solve the problem.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 20831810
OK.  As part of the closing procedure (eg in the OnDestroy) drain the message queue

      MSG msg;
      while(PeekMessage(&msg, NULL, uiMessage, uiMessage, PM_REMOVE));
where uiMessage is the message ID you want to remove
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

Author Comment

by:davinder101
ID: 20838637
Hi,
I have tried as per your instruction , but result is the same .
Here is my code for doing all this.

void CProgressDlg::OnDestroy()
{
      CDialog::OnDestroy();
      
      LPMSG            lpMsgOut;

      while(PeekMessage(lpMsgOut, this->m_hWnd , MSG_PROGRESSBAR_STEP , MSG_PROGRESSBAR_STEP, PM_REMOVE));
      
}

NOTE: We are sending  ' MSG_PROGRESSBAR_STEP ' message in PostMessage .

Thanks
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 20838802
I've just had another look and realised the ASSERT is when you are calling postmessage.  The code in the OnDestroy is irrelevant.

Before you post the message make a check
if(IsWindow(hWnd))  
  PostMessage(....)

What you are saying is the following
if(IsWindow(hWnd))  <<---At this point hWnd is a valid window
   PostMessage(....)    <<----At this point hWnd is not a valid window

Where do you use the PostMessage?  How do you the check the window is valid?
0
 

Author Comment

by:davinder101
ID: 20848756

Hi,

I am using the PostMessage in my Thread class. i call this thread form the dialog class.

What We are saying is the following
if(IsWindow(hWnd))  <<---At this point hWnd is a valid window
   PostMessage(....)    <<----At this point hWnd is valid  but it is in the Message queue

Now i terminate the thread and close the dialog . Now ASSERT failure because Message Queue looks for the dialog handle to dispatch the message.

My question is
How to drain the message queue before closing the dialog.

Thanks
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 20848794
I suspect the wrong question.
What you really need is to kill the thread as part of the dialog destruction, eg in the OnClose.


ps. I hope you aren't passing a CWnd pointer to another thread - MFC is NOT thread safe.  Some things will work without problems, others can fail.
0
 
LVL 1

Accepted Solution

by:
nksaxena earned 1000 total points
ID: 20883792
First thing, if you are going this way...

void CProgressDlg::OnDestroy()
{
      CDialog::OnDestroy();
      LPMSG   lpMsgOut;
      while(PeekMessage(lpMsgOut, this->m_hWnd , MSG_PROGRESSBAR_STEP , MSG_PROGRESSBAR_STEP, PM_REMOVE));
 }

you should first call PeekMessage and then CDialog::OnDestroy();
means,
void CProgressDlg::OnDestroy()
{
      LPMSG   lpMsgOut;
      while(PeekMessage(lpMsgOut, this->m_hWnd , MSG_PROGRESSBAR_STEP , MSG_PROGRESSBAR_STEP, PM_REMOVE));
    CDialog::OnDestroy();
 }

Other way is,
Check from your Thread for dialogs safe handle means call GetSafeHWnd(), like this,

If (pDlg->GetSafeHWnd())
   :: SendMessage //  (not Post)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

613 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