0

I am writing a little GTK3 app with C++ and gtkmm library.

In the gtkmm documentation usually some concrete instances such as Gtk::Application, Gtk::Builder, Gtk::StatusIcon etc. initialized with create() static methods that return a Glib::RefPtr. Meanwhile child widgets usually arise just on stack.

That is not clear for me:

  • Is it because memory stack usage or something else? There is no RefPtr's in my code for now. I checked usage of stack with valgrind's massif tool, peak usage was about 100 KB. Seems not too low for me, but comparable size example with RefPtr's takes a same piece of stack memory.
  • May I place all instances just on stack like Application myapp, or should I always use create() while it present?
  • What advantages the pointers provide in this case?

2 Answers 2

2

Glib::RefPtr is a reference-counted smart pointer that predates std::shared_ptr and performs the same essential functions. The use case for it is similar -- it allows multiple objects want to share ownership of an object without knowing about each other directly.

In your specific examples, an icon might be shared because it is used in multiple places in a UI, and you don't want to maintain many copies of the same image data, which could use a considerable bit of memory if there are many icons.

The Application and Builder objects are likely held by multiple objects in your program (e.g., different window or dialog objects), so the reference counting keeps each alive as long as one of those objects is still using it. This frees these users of the Application object from having to know about all the other parts of the program that might use the shared Application object. When one window is done with the Application, it destroys its smart pointer to the Application. If that window was the last owner, this also destroys the Application object, but otherwise it stays alive for the other users -- all without your destroyed window knowing about it whether the Application lives on or not.

2
  • Thank you for short and clear answer, @metal! But in this way while we talking about C++ why not just to pass references on such shared objects? Or objects' lifetime is the cause? Feb 14, 2019 at 8:12
  • Yes, it is to manage lifetime. If you pass references, they may become dangling and cause undefined behavior because some other part of the program deletes the object (or it goes out of scope, if it's on the stack). Shared ownership via reference counting allows different parts of the program to share ownership and keep it alive without knowing about each other directly.
    – metal
    Feb 14, 2019 at 12:07
1
  • Is it because memory stack usage or something else? There is no RefPtr's in my code for now. I checked usage of stack with valgrind's massif tool, peak usage was about 100 KB. Seems not too low for me, but comparable size [example with RefPtr's][1] takes a same piece of stack memory.

The main reason is that gtkmm is a thin C++ wrapper over Gtk+ library written in C. Most objects in Gtk+ are allocated on heap (eg. GtkApplication object is created using gtk_application_new function), and are reference counted (manually, using g_object_ref/g_object_unref functions). In other words, those objects have already shared pointer semantics, albeit expressed in plain C. Because of this, it makes most sense to represent those objects in C++ as smart pointers - Glib::RefPtr in this case.

  • May I place all instances just on stack like Application myapp, or should I always use create() while it present?

No, because Gtk::Application contructors are protected, so compiler won't allow to create those objects on stack.

  • What advantages the pointers provide in this case?

I think it's the other way round. Keeping those objects on stack wouldn't provide any advantages, as Gtk objects wrapped by gtkmm objects are essentially shared pointers.

3
  • Seems reasonable. But why gtkmm does not hide Glib's share pointers for some more compact and flexible code (e.g. if someone wants to use some kind of boost's pointers)? Is it because avoiding overhead? And I sill can not understand how share pointers can help me with objects which lifetime complies to program lifetime. Feb 20, 2019 at 7:24
  • It's not possible to dereference RefPtr to pass to e.g. add_window(Window &). I checked the example. And so, the example's window create method returns a pointer (just pointer, not a RefPtr). I should say it is too frustrating for me, cause I am totally confused when I should use RefPtrs, when I should use "plain" pointers and why I should not use just references. Feb 20, 2019 at 8:28
  • Well, raw/dumb pointers are generally discouraged in modern C++. You can dereference a shared_ptr exactly as you would a raw pointer. And there is no real advantage to using references, you are often not allowed to anyway, and there's a theoretical risk you could end up with dangling references and then your program will segfault.
    – Joey Sabey
    Nov 4, 2023 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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