destruction of sub-class

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
User avatar
nelson777
Earned some good credits
Earned some good credits
Posts: 117
Joined: Mon Sep 19, 2005 3:50 pm
Location: Fortaleza - Brasil

destruction of sub-class

Post by nelson777 » Thu Nov 03, 2005 1:44 pm

Hello all,

Some doubts about the desctruction of a sub-object.

I have a wxDisplay object contructed at the CreateGUIControls() method of my application

eco
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 203
Joined: Tue Aug 31, 2004 7:06 pm
Location: Behind a can of Mountain Dew
Contact:

Post by eco » Fri Nov 04, 2005 1:43 am

If you define a sub-object as an object with a parent which owns the object then yes, wxWidgets will almost always handle deletion for you. An object that is simply a member of another object does not fit this criteria.

Generally with wxWidgets it will automatically delete objects that you've specified a parent for (any control, most windows, etc.). Also, in a few cases wxWidgets documentation will state whether a certain object becomes "owned" by a wxWidget's object and will be deleted automatically. The documentation is pretty clear about these cases.

As you can see from your example, wxDisplay doesn't have a parent specified so you'll have to clean it up yourself. Objects made on the heap (with new) are never deleted automatically (in fact, if you let the only pointer to one lose scope, you've just created a memory leak). Of course, this rule doesn't follow if you've wrapped your pointer up in some kind of smart pointer (such as std::auto_ptr or a Boost shared_ptr) or, as is the frequent case with wxWidgets, there is an ownership structure that handles when an object should be deleted.

User avatar
nelson777
Earned some good credits
Earned some good credits
Posts: 117
Joined: Mon Sep 19, 2005 3:50 pm
Location: Fortaleza - Brasil

Post by nelson777 » Fri Nov 04, 2005 12:56 pm

So, as wxDisplay isn

chris
I live to help wx-kind
I live to help wx-kind
Posts: 150
Joined: Fri Oct 08, 2004 2:05 pm
Location: Europe

Post by chris » Fri Nov 04, 2005 2:29 pm

Hi,

Yes, you should, but that hasn't anything to do with derivation.
In C++, all objects created via "new" must be explicitly deleted via "delete".

wxWidgets takes some of that workload off you in some cases.
One of those cases is a control (like a wxButton) that has been attached to, say, a wxFrame. If now the frame gets destructed, the wx coders thought "Well, if this frame gets deleted, the user (=you) wants to delete all wxButtons, too. And we have pointers to those buttons, so let's do this (deleting the objects) automatically in the frame's destructor, because the user then has less to type."

Read eco's post again to see where this is the case.

HTH, Chris
this->signature=NULL;

User avatar
nelson777
Earned some good credits
Earned some good credits
Posts: 117
Joined: Mon Sep 19, 2005 3:50 pm
Location: Fortaleza - Brasil

Post by nelson777 » Fri Nov 04, 2005 6:24 pm

Well, I understand this.

What I meant for derivation was objects like wxDialog that has it

Post Reply