Discussion:
Allocation/Deallocation of wxWidgets Object
Vincent R.
2014-07-30 08:50:32 UTC
Permalink
Hi,

I am working on a new project that uses wxWidgets 2.8 framework and when I
read some code I didn't write I am wondering if it's the right thing to do.
So inside this project controls are allocated using new and deleted inside
the MainFrame destructor :

MainAppFrame::MainAppFrame(wxWindow *parent,
wxString & i_sAppNAme,
const wxString & strDeviceName,
wxWindowID id,
const wxString& title,
const wxPoint& pos,
const wxSize& size
) : MainXXXXAppFrame(parent,
i_sAppNAme,strDeviceName,"",id,title,pos,size)
{
//Controls are allocated here
m_DirectDrivePanel = new DirectDrivePanel(this); // DirectDrivePanel
inheriths from wxPanel
...
}




MainAppFrame::~MainAppFrame()
{
if (DirectDrivePanel != NULL)
delete DirectDrivePanel ;
}
From what I know normally wxWidgets object are released by the parents, so
I am wondering why the following code works and it doesn't make a double
deallocation ?
To me when I quit application I should see some crashes because object is
manually deleted , then when the frame is destroyed it should delete a
dangling pointer but this is not
what I can see. Manually deleting the control seems to work...
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Eric Jensen
2014-07-30 09:21:47 UTC
Permalink
Hello Vincent,

Wednesday, July 30, 2014, 10:50:32 AM, you wrote:

VR> Hi,

VR> I am working on a new project that uses wxWidgets 2.8 framework and when I
VR> read some code I didn't write I am wondering if it's the right thing to do.
VR> So inside this project controls are allocated using new and deleted inside
VR> the MainFrame destructor :

VR> MainAppFrame::MainAppFrame(wxWindow *parent,
VR> wxString & i_sAppNAme,
VR> const wxString & strDeviceName,
VR> wxWindowID id,
VR> const wxString& title,
VR> const wxPoint& pos,
VR> const wxSize& size
VR> ) : MainXXXXAppFrame(parent,
VR> i_sAppNAme,strDeviceName,"",id,title,pos,size)
VR> {
VR> //Controls are allocated here
VR> m_DirectDrivePanel = new DirectDrivePanel(this); // DirectDrivePanel
VR> inheriths from wxPanel
VR> ...
VR> }




VR> MainAppFrame::~MainAppFrame()
VR> {
VR> if (DirectDrivePanel != NULL)
VR> delete DirectDrivePanel ;
VR> }



VR> From what I know normally wxWidgets object are released by the parents, so
VR> I am wondering why the following code works and it doesn't make a double
VR> deallocation ?
VR> To me when I quit application I should see some crashes because object is
VR> manually deleted , then when the frame is destroyed it should delete a
VR> dangling pointer but this is not
VR> what I can see. Manually deleting the control seems to work...

If you delete a wxWindow, it detaches itself from its parent. That's
why it works and you don't get a crash.

You don't have to delete it though.

Regards,
Eric
--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+***@googlegroups.com
or visit http://groups.google.com/group/wx-users
Loading...