Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > General Discussion
  New Posts New Posts RSS Feed - CFormView Child of CDialog Causes Heap Corruption
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

CFormView Child of CDialog Causes Heap Corruption

 Post Reply Post Reply
Author
Message
AZDavidPhx View Drop Down
Newbie
Newbie


Joined: 14 August 2010
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote AZDavidPhx Quote  Post ReplyReply Direct Link To This Post Topic: CFormView Child of CDialog Causes Heap Corruption
    Posted: 14 August 2010 at 2:33pm
Somewhat new to MFC development and stumped.

I am trying to embed a CFormView as a child window into the client area of a CDialog.

My experiment is as follows (snippets / pseudo code)

class ButtonButtonPanel : public CFormView
private:
     CButton button1;
     CButton button2;

class ButtonPanelDialog : public CDialog
private:
     ButtonPanel panel;

BOOL ButtonPanel::Create( CWnd* pParent )
{
    CRect rect ;
    pParent->GetClientRect( rect ) ;

     rect.bottom = 75 ;

    return CFormView::Create( NULL, NULL, WS_CHILD | WS_VISIBLE, rect, pParent, 0, NULL ) ;
}

BOOL ButtonPanelDialog::OnInitDialog( void )
{
     __super::OnInitDialog( ) ;

     buttonPanel.Create( this ) ;

     return TRUE ;
} ;

int main( )
{     
    ButtonPanelDialog dialog ;

    dialog.DoModal( ) ;
    
    return 0 ;
}

Everything works. The call to DoModal pops up the dialog which contains the panel with the 2 buttons on it.

When I close the dialog. It crashes saying the heap has been corrupted.

Have no clue why. There is obviously something that I am not understanding. Would greatly appreciate it if someone could help me cure my ignorance.

Thanks a lot.

-David
Back to Top
AZDavidPhx View Drop Down
Newbie
Newbie


Joined: 14 August 2010
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote AZDavidPhx Quote  Post ReplyReply Direct Link To This Post Posted: 14 August 2010 at 2:36pm
Here is the actual code:


// ConsoleApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ConsoleApp.h"
#include "Resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object

CWinApp theApp;

using namespace std;

class ButtonPanel : public CFormView
{
public:
     ButtonPanel( void ) ;
     ~ButtonPanel( void ) ;

     virtual BOOL Create( CWnd* pParent ) ;

protected:
    virtual void DoDataExchange( CDataExchange* pDX ) ;

private:
     CButton button1 ;
     CButton button2 ;
} ;

ButtonPanel::ButtonPanel( void )
              :CFormView( IDD_BUTTON_PANEL )
{

} ;

ButtonPanel::~ButtonPanel( void )
{
     
} ;

BOOL ButtonPanel::Create( CWnd* pParent )
{
    CRect rect ;
    pParent->GetClientRect( rect ) ;

     rect.bottom = 75 ;

    return CFormView::Create( NULL, NULL, WS_CHILD | WS_VISIBLE, rect, pParent, 0, NULL ) ;
}

void ButtonPanel::DoDataExchange( CDataExchange* pDX )
{
    __super::DoDataExchange( pDX ) ;
    DDX_Control( pDX, IDC_BUTTON1, button1 ) ;
     DDX_Control( pDX, IDC_BUTTON2, button2 ) ;
} ;

class ButtonPanelDialog : public CDialog
{
public:
     ButtonPanelDialog( void ) ;
     ~ButtonPanelDialog( void ) ;

     virtual BOOL OnInitDialog( void ) ;

private:
     ButtonPanel buttonPanel ;
} ;

ButtonPanelDialog::ButtonPanelDialog( void )
                     : CDialog( IDD_DIALOG ),
                         buttonPanel( )
{
     
} ;

ButtonPanelDialog::~ButtonPanelDialog( void )
{
     
} ;

BOOL ButtonPanelDialog::OnInitDialog( void )
{
     __super::OnInitDialog( ) ;

     buttonPanel.Create( this ) ;

     return TRUE ;
} ;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
     int nRetCode = 0;

     // initialize MFC and print and error on failure
     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
     {
          // TODO: change error code to suit your needs
          _tprintf(_T("Fatal Error: MFC initialization failed\n"));
          nRetCode = 1;
     }
     else
     {
          ButtonPanelDialog dialog ;

          dialog.DoModal( ) ;
     }

     return nRetCode;
}
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down



This page was generated in 0.094 seconds.