Destructing Glib::RefPtr causes failed assertions in the GTK 3 core

夙愿已清 提交于 2019-12-04 04:50:33

The reference count is too low, and you can fix it by adding a ptr->reference() after ptr->show_all(). I have an explanation, but take it with a grain of salt:

  • Glib::RefPtr doesn't increment the reference count of its object initially.
  • The GtkWindow will have a reference count initially of 1.
  • When your window is closed, the library decrements the reference count of its GtkWindow once.
  • Since the GtkWindow's count is zero, it is destroyed.
  • kit.run() seeing there is no more windows, returns.
  • w goes out of scope and the RefPtr's object's count is decremented causing the error.

I can't really answer #2 or #4 unfortunately, as this area of gtk/gtkmm is still a little mysterious (to me).

Reference: http://www.gtkforums.com/viewtopic.php?t=2412

Glib::RefPtr is not meant to be for general use. You should use it when the API forces you to, but not otherwise. GtkWindow (or Gtk::Window) has its own odd memory management which is not really compatible with RefPtr.

If you want a general purpose smartpointer, try std::shared_ptr or std::unique_ptr. Or you could find something in boost.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!