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

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();      
}


jspradAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mahesh1402Commented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
I agree with mahesh, you haven't created the window, just the block of memory it will occupy.
0
jspradAuthor Commented:
Sorry, I do have a Create() call in the frame constructor:

MyPopupFrame::MyPopupFrame()
{
      Create(NULL, "Help!");
}
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

DanRollinsCommented:
Check to see that when you kill the window, you are setting theApp.helpWindow back to NULL.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
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
jspradAuthor Commented:
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
DanRollinsCommented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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
jspradAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

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.