Link to home
Start Free TrialLog in
Avatar of demarcy
demarcy

asked on

Debug Assertion Failed error

Hi

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
#else
      Enable3dControlsStatic();      // Call this when linking to MFC statically
#endif


      // 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)
{
      AddPage(&Setup);
}

CFingerScan::~CFingerScan()
{
}

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

CSetupPage::~CSetupPage()
{
}

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


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

/////////////////////////////////////////////////////////////////////////////
// CSetupPage message handlers

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

Thanks in advance
Avatar of naveenkohli
naveenkohli

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.

Avatar of demarcy

ASKER

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.

Purpose:
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
Avatar of demarcy

ASKER

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?


Avatar of demarcy

ASKER

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.

Thanks
CHECK THE PROPERTIES SET IN THE DIALOG RESOURCE .
IT MUST BE CHILD WITH NO FRAME FOR ANY DIALOGS TO BE PLACED IN PROPERTY SHEETS.
I THINK THAT IS YOUR PROBLEM
Avatar of demarcy

ASKER

It is set this way:

IDD_SETUP DIALOGEX 0, 0, 442, 196
STYLE DS_3DLOOK | WS_CHILD | WS_VISIBLE

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

Thanks
ASKER CERTIFIED SOLUTION
Avatar of speedup
speedup

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
-------------------------------------
CMainFrame::CMainFrame()
{
      // 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
thanks