I have a general question regarding a best practice on frame creation and deletion.
As I understand it, the top level frame such as wxFrame should be created in the OnInit() function of wxAPP using a statement like:
myFrame *frame_p = new myFrame(...); // where myFrame has been derived from wxFrame ...
As such, I would expect a similar approach when "closing" the top level frame. That is to say, in the OnExit() function, I should have a statement like:
delete frame_p; // where frame_p is a local member variable
However, it seems the frame would have been "deleted" when the "close" icon is clicked before the OnExit() function is called.
As such, the frame_p delete operation will failed.
I just want to get an idea from the experts here if there is a recommended practice for closing/shutting down the app.
best practice on frame creation and deletion
Re: best practice on frame creation and deletion
The default behavior of a wxFrame is to destroy itself when it's closed.
And the default behavior of wxApp is to terminate the application when there are no more toplevel windows.
So in most cases you don't have to do call "delete frame_p;". And if you ever wanted to do this, you should call frame_p->Destroy(). This will make sure that it's only destroyed when there are no more pending events for it. Otherwise you could see crashes when an event handler is called for an object that doesn't exist any longer.
Another way to cleanly terminate the application is to call wxTheApp->ExitMainLoop().
And the default behavior of wxApp is to terminate the application when there are no more toplevel windows.
So in most cases you don't have to do call "delete frame_p;". And if you ever wanted to do this, you should call frame_p->Destroy(). This will make sure that it's only destroyed when there are no more pending events for it. Otherwise you could see crashes when an event handler is called for an object that doesn't exist any longer.
Another way to cleanly terminate the application is to call wxTheApp->ExitMainLoop().
Use the source, Luke!
Re: best practice on frame creation and deletion
You have provided great information on the subject. Thank you.
Just a few follow ups:
1. if there is a requirement to collect and persist any config data, would it make sense to do it in the OnExit() function? If so, it would make sense to call ExitMainLoop() when the TLW's close icon is clicked, right? This way, all the objects would still be available so that config data could be retrieved from those objects before they are deleted. (assuming that some persistent data are kept with various objects.)
2. From an OO programming pov, does it make sense to call the destroy() function in the object's destructor? This way, a "delete frame_p;" will actually destroy all its children of the frame from the frame's destructor before deleting itself.
Just a few follow ups:
1. if there is a requirement to collect and persist any config data, would it make sense to do it in the OnExit() function? If so, it would make sense to call ExitMainLoop() when the TLW's close icon is clicked, right? This way, all the objects would still be available so that config data could be retrieved from those objects before they are deleted. (assuming that some persistent data are kept with various objects.)
2. From an OO programming pov, does it make sense to call the destroy() function in the object's destructor? This way, a "delete frame_p;" will actually destroy all its children of the frame from the frame's destructor before deleting itself.
Re: best practice on frame creation and deletion
@1) From an OOP point of view, i would put any persistence related code into the destructor of each individual object.
This might also be interesting: https://docs.wxwidgets.org/trunk/overvi ... tence.html
@2) That wouldn't work. If you call Destroy(), wxWidgets itself will call delete on the pointer later.
This might also be interesting: https://docs.wxwidgets.org/trunk/overvi ... tence.html
@2) That wouldn't work. If you call Destroy(), wxWidgets itself will call delete on the pointer later.
Use the source, Luke!