Question

gtkmm provides lifetime management of widgets using this sort of construct:

Gtk::Widget* aWidget = Gtk::manage(new Widget()); 

Gtk::Widget containerWidget;

containerWidget.add(*aWidget);

This delegates lifetime management of aWidget to containerWidget. When containerWidget is cleaned up, it also cleans up aWidget - similar to Delphi's 'owner' concept.

We also have several types of smart pointers, particular the C++ 11 smart pointers templates, which I use everywhere.

I find the manage/add syntax easier and cleaner to use, but it's not part of the C++ Standard, it's a gtkmm specific feature, which makes me think I should stick to std::shared_ptr, etc.

So I'm wondering what are the advantages/disadvantages of std smart pointers vs the gtkmm manage/add model (aside from cases where you need the reference after the owner container has been deleted, or when you have a top level widget that has no containing widget).

Was it helpful?

Solution

I don’t think it's practical to use shared_ptr for widgets if you don't need to keep a reference to that widget after you add it. You are better off having a managed widget instead using shared_ptr.

The nice thing about the gtk::manage is that you can create the object on the heap and add it to the container and forget about it, and when that container is destroyed it will destroy the widget.

As a general rule I create my widgets managed if I don't need to reference them after I add them to a container, if I do need to reference them, I allocate a pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top