wxWidgets memory management

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
zambizzi
Earned some good credits
Earned some good credits
Posts: 115
Joined: Mon Jan 03, 2005 9:16 pm
Location: Boise Idaho
Contact:

wxWidgets memory management

Post by zambizzi »

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
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Re: wxWidgets memory management

Post by Ryan Norton »

zambizzi 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
Only widgets/windows are deleted automagically... anything else you need to delete yourself if you allocate it :)
[Mostly retired moderator, still check in to clean up some stuff]
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

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
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
zambizzi
Earned some good credits
Earned some good credits
Posts: 115
Joined: Mon Jan 03, 2005 9:16 pm
Location: Boise Idaho
Contact:

Post by zambizzi »

Ahh, I figured it was just the widgets given the examples I've been looking at.

Thanks guys!
tsaphah
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Jul 04, 2005 1:31 am

Manual Memory Clean-up

Post by tsaphah »

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'
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

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
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
prophet
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Sep 01, 2004 6:19 am

Post by prophet »

Consider using the following template:

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
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Neat template!

Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
prophet
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Sep 01, 2004 6:19 am

Post by prophet »

Yeah, something similar can be done for wxMutex. Which is what wxMutexLocker does, but than in the form of a template.

Maybe i should add this to the wiki.
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

Or to the code dump section. Always handy for others...

Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
tsaphah
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Jul 04, 2005 1:31 am

Thanks

Post by tsaphah »

Thanks for the reply guys. Guess I'll leave memory management up to the system that knows a bit better =)
Post Reply