• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 830
  • Last Modified:

Debug Assertion Failed error


I'm new to programming in Visual C++ so I need some help. I am a BC++ programmer and have used OWL long time, but I want to learn MFC now.

I want to program an application who has only a Property Sheet. I have followed an example of the Kruglinski's book and I have only made one Property Page. The fact is that when I run the program, it sends the message Debug Assertion Failed! Program F:\FMAN\DEBUG\FMAN.EXE  File: afxwin1.inl  Line:22

I have to press the Abort button.
How can I solve it? this is second time that the same error occures but with different applications.

Here is a part of the main application code:
CFman::CFman():Opciones("Manejador de Unidades Fingerscan")
      // TODO: add construction code here,
      // Place all significant initialization in InitInstance

// The one and only CFman object

CFman theApp;

// CFman initialization

BOOL CFman::InitInstance()
      // Standard initialization
      // If you are not using these features and wish to reduce the size
      //  of your final executable, you should remove from the following
      //  the specific initialization routines you do not need.

#ifdef _AFXDLL
      Enable3dControls();                  // Call this when using MFC in a shared DLL
      Enable3dControlsStatic();      // Call this when linking to MFC statically

      // Since the dialog has been closed, return FALSE so that we exit the
      //  application, rather than start the application's message pump.
      return FALSE;

Here's my property sheet class:
CFingerScan::CFingerScan(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
      :CPropertySheet(nIDCaption, pParentWnd, iSelectPage)

CFingerScan::CFingerScan(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
      :CPropertySheet(pszCaption, pParentWnd, iSelectPage)


And here's the property page class:
CSetupPage::CSetupPage() : CPropertyPage(CSetupPage::IDD)
            // NOTE: the ClassWizard will add member initialization here


void CSetupPage::DoDataExchange(CDataExchange* pDX)
            // NOTE: the ClassWizard will add DDX and DDV calls here

BEGIN_MESSAGE_MAP(CSetupPage, CPropertyPage)
            // NOTE: the ClassWizard will add message map macros here

// CSetupPage message handlers

That's all, as you can see, is very simple, what is wrong with this code?

Thanks in advance
1 Solution
When ASSERTION message comes.. don't hit abort. Hit Retry and then see where in your code the assertion happened..

What i suspect is that u have called a function on invalid window handle.

From what i can see... InitInstance function doe snot seem to be complete. This is the place where u property sheet will be created and set as mainwindow of the application.

See where exacly this assertion is coming from. Then u can add some more info to your question..
It seems to be a Dialog based application.
Can you please tell me when exactly (Line) you get the assertion failure.

demarcyAuthor Commented:
Well... I have got that error twice, the first time was on a dialog based application. I have continued trying to depelop it since it's the best for my purposes, so let me explain more about the code.

To have a dialog box (the application) which has no buttons, for obvious reasons. To exit it, the user will need only to press the cross button on the top right corner. On that dialog, I have a Tab Control, IDC_OPTIONS, inside that control I want to place a child dialog box depending on the tab selected. I have only one dialog created now, the one to be placed on the first tab, IDD_SETUP. In Borland OWL I only call the constructor for the dialog (in the constructor for the FrameWindow object) and then use ShowWindow method with SW_SHOW attribute to show the window (in the SetupWindow method). I have tried to do almost the same in MFC but I got those strange errors.

Line where assertion is launched:
Opciones = (CTabCtrl *)this->GetDlgItem( IDC_OPTIONS );

I debug when assertion comes and the application continues over that instruction, so I guess the problem is there.
By debugging, after the call to GetDlgItem method returns a value of 0x00000000, so I think Opciones isn't being created well.

Where is the problem? do you have a program that uses a Tab Control? that way I would be able to study the right way to do it.

Please don't suggest me to use a PropertySheet and PropertyPages classes instead. I want to use a Tab Control :)

Thanks a lot
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

demarcyAuthor Commented:
Ah! and of course I have just before Opciones = (CTabCtrl *)this->GetDlgItem( IDC_OPTIONS );  the call of the constructor this way Opciones = new CTabCtrl( );

I use this opportunity to ask you in what cases I define an object as a pointer and in what cases as a normal variable. At the moment, I have declared Opciones as CTabCtrl*. In some examples I have seen declarations like CtabCtrl Opciones; in the last case, methods and member variables are called using . instead of ->. The method GetDlgItem can only return a pointer to the object?

Thanks again
where exactly the code has been given.(In which message handler)
Can you get the handle of this pointer?

demarcyAuthor Commented:
I have realized that it isn't the same thing to call an initialization method from one or other method. I placed Opciones = (CTabCtrl *)this->GetDlgItem( IDC_OPTIONS ); in the dialog constructor and an assertion was launched. I placed it on the onInitDialog and after it, I could safely use InsertItem, and at least when I run the application, the Tab control is shown with the corresponding added tabs.

The problem now is when I try to show a dialog according to the tab selected. An assertion accures again. These are some questions refering to the way to do it:
1) Do I have to declare the child dialog (to be placed when a tab is selected) as CSetupPage* Setup, or CSetupPage Setup. That is, as a pointer or not.
2) How and where do I have to call the constructor to assign the resource ID with the object. That dialog is child of the TabControl, so I'm trying Setup = new CSetupPage( Opciones ) on the onInitDialog of the main dialog, because Opciones is there assigned.
3) Where and how do I have to show the dialog. I cannot use DoModal, since the dialog is a child. (I have tried it, I have used DoModal in the onPaint method, that way the application doesn't fail, but the dialog is shown as modal and under the main window. If I use ShowWindow method, assertion comes.

When an assertion error arrives, I realized that hWnd variable is 0x00000000 so the dialog isn't created well.

I hope you can help me.. if you don't understand something or need more information, please post a comment. I could even post the whole code.. it is too simple and too small, so it is no problem.

demarcyAuthor Commented:
It is set this way:

IDD_SETUP DIALOGEX 0, 0, 442, 196

So, it has no border and it's a child dialog. When I worked with OWL, I set the dialog that way and it worked

most of the codes created by classwizard as you list them above.
the most possible cause of the assertion failure is the null pointer.
the following steps can help you find the bug:
1. press debug or retry button when assertion failure appears.
2. the highlight bar stops where the null pointer is refered. try to check if the pointer isn't allocated.
3. if the pointer points to a block of memory, go to the contruction of the class ( for example, new MyClass() ). put breakpoint on it.
4. run the program again. and it breaks where you put breakpoint. press F11 to trace into the codes to see where the problem is.

and you can list the related codes here to make clarity for me.
I have done so and I found that the program fails on an instruction that uses de hWnd, I think it's the hWnd of the control I'm trying to show (it is 0x000000). I could finally placed the tab control, but the font by default is too ugly, so I will try to learn how to change the font. But the main problem is that when I run the application, the hourglass cursor stays for long, so I don't know if I'm doing well when I created the tab control, for that reason  I have asked for some examples of using tabctrl.

This is where I create the control
      // TODO: add member initialization code here
      Opciones = new COpciones();
Here is where I make it visible

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
      RECT rect;

      if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
            return -1;
      // create a view to occupy the client area of the frame
      if (!m_wndView.Create( NULL, NULL, AFX_WS_DEFAULT_VIEW,
            CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
            TRACE0("Failed to create view window\n");
            return -1;
      rect.left   = 0;
      rect.top    = 0;
      rect.right  = 0;
      rect.bottom = 0;
      Opciones->Create(TCS_TABS|WS_VISIBLE|WS_TABSTOP, CRect(10, 10, 20, 20), this, AFX_IDW_PANE_FIRST);
      Opciones->InsertItem(0, "Configuración");
      Opciones->InsertItem(1, "Directorios");
      Opciones->InsertItem(2, "Transacciones");
      Opciones->InsertItem(3, "Usuarios");
      Opciones->InsertItem(4, "Acerca de...");
      return 0;

The TRect parameter doesn't work, so I cannot control de size and position of the control (????????). The control is shown if and only if I use AFX_IDW_PANE_FIRST as the ID of the control.

Any further help will be appreciated
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: 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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now