Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

gtkmm provides lifetime management of widgets using

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


This delegates lifetime management of aWidget to its container widget.

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 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).

share|improve this question
up vote 2 down vote accepted

Grk::manage() solves the specific problem of lifetime management for hierarchies of widgets. And it solves it well.

Smart pointers (shared_ptr in particular) have broader application range, and therefore will be less efficient when used to address this specific problem. Lifetime management for hierarchies of widgets can be solved with shared_ptr, but it will be:

  • not as concise as using manage() (as you pointed out yourself);
  • less efficient in terms of memory usage, since using shared_ptr's will introduce memory overhead as shared_ptr memory footprint is going to contribute to your widget's size (as opposed to manage(), which as far as I'm aware uses variables, such as, say Gtk::Object.referenced_, that are part of your Widget base class anyways, no matter you use manage() on it or not); So in case you have significant amount of Widgets it might become an issue worth considering;
  • not as mainstream as using manage() in Gtk (has quite a few consequences, including clarity and maintainability).

As of idea of favoring shared_ptr over manage() because it is part of C++ Standard, while Gtk::manage is not - I'm not sure it is going to be a game changer for average application, as by not using manage() you don't cut your dependency on Gtk anyways. So your application is not going to gain any better portability if you'd go for shared_ptr.

I would rather leverage native Gtk API, for sake of clarity and efficiency.

p.s. there is actually a smart pointer GLib::RefPtr, which can address lifetime management for Widgets in a manual way. Again, as it is a feature native to GLib it leverages built-in facilities of Glib::ObjectBase, and therefore more efficient then shared_ptr for certain applications (e.g. reference to reference counter is not stored in the pointer object, as reference counter is embedded to the object, and therefore RefPtr is more lightweight than shared_ptr, which will make a significant difference if you have many pointers to a few objects).

share|improve this answer
Excellent, thorough answer. – Vector Mar 1 '14 at 20:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.