MFC Assertion Failure

This is a discussion on MFC Assertion Failure within the Windows Programming forums, part of the Platform Specific Boards category; Hey all. I was trying to write and run an MFC program that displays a fractal, and I seem to ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    46

    MFC Assertion Failure

    Hey all. I was trying to write and run an MFC program that displays a fractal, and I seem to be having a bit of a problem. After the fist iteration through, it blows up! I was wondering if you people had any ideas.

    thanks!!

    -maxthecat

    The listing is rather long, since I included all of the files, but I am sure the problem is in the implementation of the Timer function in ChildView.cpp ...If anyone would rather have me simply send them the workspace and files, just email me, maxthecat.geo@yahoo.com ...Thanks a ton for the help! (It's been driving me nuts! I've even tried to debug it step by step, going through the AfxWin files...)

    /**************************************************
    ChildView.cpp
    **************************************************/

    // ChildView.cpp : implementation of the CChildView class
    //

    #include "stdafx.h"
    #include "mfc.h"
    #include "ChildView.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CChildView

    CChildView::CChildView()
    {
    }

    CChildView::~CChildView()
    {
    }


    BEGIN_MESSAGE_MAP(CChildView,CWnd )
    //{{AFX_MSG_MAP(CChildView)
    ON_WM_PAINT()
    ON_WM_SIZE()
    ON_WM_TIMER()
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()


    /////////////////////////////////////////////////////////////////////////////
    // CChildView message handlers

    BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs)
    {
    if (!CWnd::PreCreateWindow(cs))
    return FALSE;

    cs.dwExStyle |= WS_EX_CLIENTEDGE;
    cs.style &= ~WS_BORDER;
    cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCL KS,
    ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW+1), NULL);

    return TRUE;
    }

    void CChildView::OnPaint()
    {

    CPaintDC dc(this); // device context for painting

    // TODO: Add your message handler code here

    // Do not call CWnd::OnPaint() for painting messages
    }


    void CChildView::OnSize(UINT nType, int cx, int cy)
    {
    CWnd ::OnSize(nType, cx, cy);
    CX = cx / 2;
    CY = cy / 2;
    // TODO: Add your message handler code here

    }

    void CChildView::OnTimer(UINT nIDEvent)
    {
    // TODO: Add your message handler code here and/or call default
    CClientDC dc(this);
    if (flag == 1)
    {
    dc.MoveTo(CX, CY);
    dc.LineTo (CX, (CY + SCALE));
    CY += SCALE;
    dc.MoveTo (CX, CY);
    dc.LineTo ((CX - SCALE), CY);
    CX -= SCALE;
    dc.MoveTo (CX, CY);
    flag = 0;
    }
    else
    {
    b = count - 1;
    for (b;b>0;b--,a++)
    {
    if (array[b] == 3)
    array.Add (0);
    array.Add ((b + 1));
    switch (array[a])
    {
    case 0:
    dc.LineTo (CX, (CY + SCALE));
    CY += SCALE;
    dc.MoveTo (CX, CY);
    break;
    case 1:
    dc.LineTo ((CX - SCALE), CY);
    CX -= SCALE;
    dc.MoveTo (CX, CY);
    break;
    case 2:
    dc.LineTo (CX, (CY - SCALE));
    CY -= SCALE;
    dc.MoveTo (CX, CY);
    break;
    case 3:
    dc.LineTo ((CX + SCALE), CY);
    CX += SCALE;
    dc.MoveTo (CX, CY);
    break;
    }
    }
    count = count * count;
    }
    CWnd ::OnTimer(nIDEvent);
    }

    int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CWnd ::OnCreate(lpCreateStruct) == -1)
    return -1;
    array.SetSize (2);
    array[0] = 0;
    array[1] = 1;
    a = 2;
    b = 0;
    count = 2;
    SCALE = 10;
    flag = 1;
    SetTimer(1, 1000, NULL);
    // TODO: Add your specialized creation code here

    return 0;
    }

    /**********************************************
    MainFrm.cpp
    ***********************************************/

    // MainFrm.cpp : implementation of the CMainFrame class
    //

    #include "stdafx.h"
    #include "mfc.h"

    #include "MainFrm.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame

    IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    //{{AFX_MSG_MAP(CMainFrame)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    // DO NOT EDIT what you see in these blocks of generated code !
    ON_WM_SETFOCUS()
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame construction/destruction

    CMainFrame::CMainFrame()
    {
    // TODO: add member initialization code here

    }

    CMainFrame::~CMainFrame()
    {
    }

    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    if( !CFrameWnd::PreCreateWindow(cs) )
    return FALSE;
    // TODO: Modify the Window class or styles here by modifying
    // the CREATESTRUCT cs

    cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
    cs.lpszClass = AfxRegisterWndClass(0);
    return TRUE;
    }

    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame diagnostics

    #ifdef _DEBUG
    void CMainFrame::AssertValid() const
    {
    CFrameWnd::AssertValid();
    }

    void CMainFrame:ump(CDumpContext& dc) const
    {
    CFrameWnd:ump(dc);
    }

    #endif //_DEBUG

    /////////////////////////////////////////////////////////////////////////////
    // CMainFrame message handlers
    void CMainFrame::OnSetFocus(CWnd* pOldWnd)
    {
    // forward focus to the view window
    m_wndView.SetFocus();
    }

    BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
    {
    // let the view have first crack at the command
    if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
    return TRUE;

    // otherwise, do default handling
    return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
    }

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
    return -1;
    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 window\n");
    return -1;
    }
    return 0;
    }

    /*****************************************
    mfc.cpp
    *****************************************/
    // mfc.cpp : Defines the class behaviors for the application.
    //

    #include "stdafx.h"
    #include "mfc.h"

    #include "MainFrm.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CMfcApp

    BEGIN_MESSAGE_MAP(CMfcApp, CWinApp)
    //{{AFX_MSG_MAP(CMfcApp)
    ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    // DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CMfcApp construction

    CMfcApp::CMfcApp()
    {
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
    }

    /////////////////////////////////////////////////////////////////////////////
    // The one and only CMfcApp object

    CMfcApp theApp;

    /////////////////////////////////////////////////////////////////////////////
    // CMfcApp initialization

    BOOL CMfcApp::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.

    // Change the registry key under which our settings are stored.
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization.
    SetRegistryKey(_T("Local AppWizard-Generated Applications"));


    // To create the main window, this code creates a new frame window
    // object and then sets it as the application's main window object.

    CMainFrame* pFrame = new CMainFrame;
    m_pMainWnd = pFrame;

    // create and load the frame with its resources

    pFrame->LoadFrame(IDR_MAINFRAME,
    WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
    NULL);




    // The one and only window has been initialized, so show and update it.
    pFrame->ShowWindow(SW_SHOW);
    pFrame->UpdateWindow();

    return TRUE;
    }

    /////////////////////////////////////////////////////////////////////////////
    // CMfcApp message handlers





    /////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App About

    class CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();

    // Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    //}}AFX_VIRTUAL

    // Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    // No message handlers
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };

    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }

    void CAboutDlg:oDataExchange(CDataExchange* pDX)
    {
    CDialog:oDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }

    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    // App command to run the dialog
    void CMfcApp::OnAppAbout()
    {
    CAboutDlg aboutDlg;
    aboutDlg.DoModal();
    }

    /////////////////////////////////////////////////////////////////////////////
    // CMfcApp message handlers


    /********************************************
    ChildView.h
    *************************************/

    // ChildView.h : interface of the CChildView class
    //
    /////////////////////////////////////////////////////////////////////////////

    #if !defined(AFX_CHILDVIEW_H__F98A6FCA_2F8A_11D6_951A_ 444553540000__INCLUDED_)
    #define AFX_CHILDVIEW_H__F98A6FCA_2F8A_11D6_951A_444553540 000__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    /////////////////////////////////////////////////////////////////////////////
    // CChildView window

    class CChildView : public CWnd
    {
    // Construction
    public:
    CChildView();

    // Attributes
    public:

    // Operations
    public:

    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CChildView)
    protected:
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    //}}AFX_VIRTUAL

    // Implementation
    public:
    int flag;
    int SCALE;
    int a, b;
    CUIntArray array;
    int count;
    int CY;
    int CX;

    virtual ~CChildView();

    // Generated message map functions
    protected:
    //{{AFX_MSG(CChildView)
    afx_msg void OnPaint();
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnTimer(UINT nIDEvent);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };

    /////////////////////////////////////////////////////////////////////////////

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_CHILDVIEW_H__F98A6FCA_2F8A_11D6_951A_ 444553540000__INCLUDED_)


    /*************************************
    MainFrm.h
    *************************************/
    // MainFrm.h : interface of the CMainFrame class
    //
    /////////////////////////////////////////////////////////////////////////////

    #if !defined(AFX_MAINFRM_H__F98A6FC8_2F8A_11D6_951A_44 4553540000__INCLUDED_)
    #define AFX_MAINFRM_H__F98A6FC8_2F8A_11D6_951A_44455354000 0__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #include "ChildView.h"

    class CMainFrame : public CFrameWnd
    {

    public:
    CMainFrame();
    protected:
    DECLARE_DYNAMIC(CMainFrame)

    // Attributes
    public:

    // Operations
    public:

    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMainFrame)
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
    //}}AFX_VIRTUAL

    // Implementation
    public:
    virtual ~CMainFrame();
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endif
    CChildView m_wndView;

    // Generated message map functions
    protected:
    //{{AFX_MSG(CMainFrame)
    afx_msg void OnSetFocus(CWnd *pOldWnd);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    // NOTE - the ClassWizard will add and remove member functions here.
    // DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };

    /////////////////////////////////////////////////////////////////////////////

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_MAINFRM_H__F98A6FC8_2F8A_11D6_951A_44 4553540000__INCLUDED_)

    /***************************************
    mfc.h
    ***************************************/
    // mfc.h : main header file for the MFC application
    //

    #if !defined(AFX_MFC_H__F98A6FC4_2F8A_11D6_951A_444553 540000__INCLUDED_)
    #define AFX_MFC_H__F98A6FC4_2F8A_11D6_951A_444553540000__I NCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #ifndef __AFXWIN_H__
    #error include 'stdafx.h' before including this file for PCH
    #endif

    #include "resource.h" // main symbols

    /////////////////////////////////////////////////////////////////////////////
    // CMfcApp:
    // See mfc.cpp for the implementation of this class
    //

    class CMfcApp : public CWinApp
    {
    public:
    CMfcApp();

    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMfcApp)
    public:
    virtual BOOL InitInstance();
    //}}AFX_VIRTUAL

    // Implementation

    public:
    //{{AFX_MSG(CMfcApp)
    afx_msg void OnAppAbout();
    // NOTE - the ClassWizard will add and remove member functions here.
    // DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };


    /////////////////////////////////////////////////////////////////////////////

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_MFC_H__F98A6FC4_2F8A_11D6_951A_444553 540000__INCLUDED_)


    /****************************************
    resource.h
    ****************************************/
    //{{NO_DEPENDENCIES}}
    // Microsoft Developer Studio generated include file.
    // Used by mfc.rc
    //
    #define IDD_ABOUTBOX 100
    #define IDR_MAINFRAME 128
    #define IDR_MFCTYPE 129
    #define IDD_DIALOG1 130
    #define IDC_BUTTON 32771

    // Next default values for new objects
    //
    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS
    #define _APS_NEXT_RESOURCE_VALUE 131
    #define _APS_NEXT_COMMAND_VALUE 32772
    #define _APS_NEXT_CONTROL_VALUE 1000
    #define _APS_NEXT_SYMED_VALUE 101
    #endif
    #endif

    /********************************
    StdAfx.h
    ********************************/

    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //

    #if !defined(AFX_STDAFX_H__F98A6FC6_2F8A_11D6_951A_444 553540000__INCLUDED_)
    #define AFX_STDAFX_H__F98A6FC6_2F8A_11D6_951A_444553540000 __INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

    #include <afxwin.h> // MFC core and standard components
    #include <afxext.h> // MFC extensions
    #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
    #ifndef _AFX_NO_AFXCMN_SUPPORT
    #include <afxcmn.h> // MFC support for Windows Common Controls
    #endif // _AFX_NO_AFXCMN_SUPPORT


    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_STDAFX_H__F98A6FC6_2F8A_11D6_951A_444 553540000__INCLUDED_)

    /***********************************************
    StdAfx.cpp
    ***********************************************/
    // stdafx.cpp : source file that includes just the standard includes
    // mfc.pch will be the pre-compiled header
    // stdafx.obj will contain the pre-compiled type information

    #include "stdafx.h"

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    Watch out for those smiles in the code.... The board seems to think that any semicolon and "D"should be a happy face.. I guess my code is just too happy :P

    -maxthecat

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: MFC Assertion Failure

    Originally posted by maxthecat
    ...If anyone would rather have me simply send them the workspace and files, just email me,
    Can you zip all files (including the project/workspace) and attach it to the thread?

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    Here's the entire project zipped up.

    Thanks!!

    -maxthecat
    Attached Files Attached Files

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I think the problem is your array. Set the timer interval to 10 seconds. This will give you the opportunity to debug the application.

    My debug results:
    Code:
    _AFXCOLL_INLINE UINT& CUIntArray::ElementAt(int nIndex)
      { ASSERT(nIndex >= 0 && nIndex < m_nSize);
        return m_pData[nIndex]; }
    nIndex = 3
    m_nSize = 3

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    Thanks, but I'm still not seeing why this should be failing the assertion... Moreover, it shouldn't be drawing a line from the center of the client area to the left hand corner... very curious... any thoughts?

    -maxthecat

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 03:47 AM
  2. Assertion failure taking place of a try-catch
    By Mario F. in forum C++ Programming
    Replies: 8
    Last Post: 06-11-2006, 09:48 AM
  3. Assertion failure while creating window
    By roktsyntst in forum Windows Programming
    Replies: 0
    Last Post: 02-10-2003, 07:18 PM
  4. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 06:02 PM
  5. WIndows programming?
    By hostensteffa in forum Windows Programming
    Replies: 7
    Last Post: 06-07-2002, 08:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21