wxWidgets memory management
-
- Earned some good credits
- Posts: 115
- Joined: Mon Jan 03, 2005 9:16 pm
- Location: Boise Idaho
- Contact:
wxWidgets memory management
I've been told that wxWidgets will do its own cleanup as far as creating/deleting objects, is this true in every case or just w/ some widgets?
For example, with wxConfig:
http://wxwidgets.org/manuals/2.4.2/wx72 ... configbase
You'll notice in the example that it is being deleted explicitly, is this just to write changes back to the file? Is it optional?
Is this because it is dealing w/ the filesystem?
What objects need to be deleted explicitly?
Thanks!
-v
For example, with wxConfig:
http://wxwidgets.org/manuals/2.4.2/wx72 ... configbase
You'll notice in the example that it is being deleted explicitly, is this just to write changes back to the file? Is it optional?
Is this because it is dealing w/ the filesystem?
What objects need to be deleted explicitly?
Thanks!
-v
- Ryan Norton
- wxWorld Domination!
- Posts: 1319
- Joined: Mon Aug 30, 2004 6:01 pm
Re: wxWidgets memory management
Only widgets/windows are deleted automagically... anything else you need to delete yourself if you allocate itzambizzi wrote:I've been told that wxWidgets will do its own cleanup as far as creating/deleting objects, is this true in every case or just w/ some widgets?
For example, with wxConfig:
http://wxwidgets.org/manuals/2.4.2/wx72 ... configbase
You'll notice in the example that it is being deleted explicitly, is this just to write changes back to the file? Is it optional?
Is this because it is dealing w/ the filesystem?
What objects need to be deleted explicitly?
Thanks!
-v
[Mostly retired moderator, still check in to clean up some stuff]
as far as I know, all wxWindow derived classes are deleted by the framework. Also, wxOBJ_ARRAY elements will be deleted because they are owned by the obj-array. Besides that I don't know if there are more classes.
EDIT to my own post: When the wxWindow is owned by a parent it will be deleted automatically. Just for future reference
- Jorgen
EDIT to my own post: When the wxWindow is owned by a parent it will be deleted automatically. Just for future reference
- Jorgen
Last edited by Jorg on Sun Feb 06, 2005 10:31 pm, edited 1 time in total.
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Manual Memory Clean-up
In line with this tread..
How much will wxWidgets hate me if I clean-up my own controls, windows, etc??
I really dislike seeing 'new' without a clearly visible 'delete'
How much will wxWidgets hate me if I clean-up my own controls, windows, etc??
I really dislike seeing 'new' without a clearly visible 'delete'
It will hate you. Well maybe not 99% of the time, but ...
Consider you will delete a wxButton / wxTextCtrl and somewhere a pending event is still in the queue from this control? You delete the control, and the event gets processed. This can result in a crash.
The reason why Destroy() must be used instread of "delete" is exactly that. It will schedule objects for deletion, and remove them when no pending events are in the queue.
Regards,
- Jorgen
Consider you will delete a wxButton / wxTextCtrl and somewhere a pending event is still in the queue from this control? You delete the control, and the event gets processed. This can result in a crash.
The reason why Destroy() must be used instread of "delete" is exactly that. It will schedule objects for deletion, and remove them when no pending events are in the queue.
Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Consider using the following template:
It is the equivalant of the std::auto_ptr, but it calls delete when the class goes out-of-scope. Us like:
Code: Select all
template<class wxtype>
class wx_ptr
{
wxtype *object;
public:
wx_ptr(wxtype *my_object = NULL)
: object(my_object)
{ }
~wx_ptr()
{
if(object != NULL)
object->Destroy();
}
void operator=(wx_ptr &other_ptr)
{
if(this == &other_ptr)
return;
if(object != NULL)
object->Destroy();
object = other_ptr.object;
other_ptr.object = NULL;
}
void reset(wxtype *my_object = NULL)
{
if(object != NULL)
object->Destroy();
object = my_object;
}
wxtype *operator->() { return object; }
wxtype const *operator->() const { return object; }
wxtype &operator*() { return *object; }
wxtype const &operator*() const { return *object; }
wxtype *get() { return object; }
wxtype const *get() const { return object; }
};
It is the equivalant of the std::auto_ptr, but it calls delete when the class goes out-of-scope. Us like:
Code: Select all
{
wx_ptr<wxMessageDialog> ptr(new wxMessageDialog(..))
ptr->ShowModal(...)
} // Ptr goes out of scope, auto delete
Neat template!
Regards,
- Jorgen
Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Or to the code dump section. Always handy for others...
Regards,
- Jorgen
Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb