?
Solved

"Debug assertion failed" when checking if frame pointer == NULL

Posted on 2006-04-10
9
Medium Priority
?
1,066 Views
Last Modified: 2013-11-20
I'm working on a simple app based on the boilerplate MDI code. I added a button to the About dialog, and in the code for that button, I open another window. I only want to have one instance of the window open at any given time. So I have a pointer to my frame class inside my main application header. If it's null, I create a new frame. If not, I just bring the current one to the front.

This works fine for the first window, but if I close it, then click the button again, I get a "debug assertion failed" error. I tried some test code and narrowed it down to the line where I check if the pointer is null (indicated below).

My question is:
a) Why does it crash just from checking if the pointer is null?
b) How do I do this correctly?

Debug Assertion Failed!
Program: C:\MFC Test\Html Popup\Debug\Html Popup.exe
File: winocc.cpp
Line: 301

Here's my pointer declaration:

public:
      MyPopupFrame* helpWindow;

And here's my function:

void CAboutDlg::OnPushme()
{
      if (theApp.helpWindow == NULL) {  // <-- This test is causing the error
            theApp.helpWindow = new MyPopupFrame;
      }
      theApp.helpWindow->ShowWindow(SW_NORMAL);
      theApp.helpWindow->SetFocus();      
}


0
Comment
Question by:jsprad
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16418410
Assertion you are getting is in ShowWindow(..) function.. tale a look at  winocc.cpp at line 301

ASSERT(::IsWindow(m_hWnd)); <== winocc.cpp line 301

it means  your window is not created. You are using ShowWindow() for MyPopupFrame.. Probably You  need CreateWindow for this one .. or just with new your window gets constructed ??

MAHESH
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16419570
I agree with mahesh, you haven't created the window, just the block of memory it will occupy.
0
 

Author Comment

by:jsprad
ID: 16420847
Sorry, I do have a Create() call in the frame constructor:

MyPopupFrame::MyPopupFrame()
{
      Create(NULL, "Help!");
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 16421751
Check to see that when you kill the window, you are setting theApp.helpWindow back to NULL.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16423934
I bet you are closing the 'help' window with a standard mechanism (OK/ESC button, close on menu, x at top right of frame) and letting windows really destroy the window (instead of hiding it).  Then as Dan suggests you are leaving the  theApp.helpWindow as non-NULL.
0
 

Author Comment

by:jsprad
ID: 16436511
I added a bit of code in PostNcDestroy to set the pointer back to NULL, and that took care of the assertions. But going back to the first part of my question, why should simply checking to see if the pointer is NULL cause an error? I would expect that to be a safe operation. Why isn't it?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 16437498
I'd say that the debugger is pointing to the wrong line -- or perhaps you are misreading what it is telling you.  

The assertion that you indicated is caused by your call to ShowWindow().

-- Dan
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 16438119
Have you put a breakpoint (F9 key) on the line
    if (theApp.helpWindow == NULL) {  // <-- This test is causing the error
and single stepped to check if that is the line that fails.
0
 

Author Comment

by:jsprad
ID: 16439594
Ah, you're right. I could've sworn it was coming from the pointer test. Accepted the first answer to recommend putting the pointer back to NULL.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
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.
Suggested Courses

809 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