CFormView Child of CDialog Causes Heap Corruption
Printed From: Codejock Forums
Category: Codejock Products
Forum Name: General Discussion
Forum Description: Topics Related to Visual C++ MFC Development in General
URL: http://forum.codejock.com/forum_posts.asp?TID=17115
Printed Date: 06 February 2025 at 2:05pm Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com
Topic: CFormView Child of CDialog Causes Heap Corruption
Posted By: AZDavidPhx
Subject: CFormView Child of CDialog Causes Heap Corruption
Date 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
|
Replies:
Posted By: AZDavidPhx
Date 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;
}
|
|