How to correctly (and effectively) release memory in a gtk widget

心已入冬 提交于 2019-12-05 07:54:53
 - g_object_ref

  Increases ref count by one

 - g_object_unref

  Decreases ref count by one, if ref count == 0, the object is destroyed

 - g_object_ref_sink

  IF the object has a floating ref, it converts that reference to a normal ref (sinks it)
  ELSE it increases the ref count by one

 - All objects start with a floating ref count of 1

For some further reading, I would suggest you take a look at the following article: Introduction to Memory Management in GTK+

Now, moving on to your example, lets look at the function calls and what they do:

GtkWidget * widget = gtk_fixed_new(); //widget created with ref count of 1 | floating = true
g_object_ref(widget); // floating = true, ref count increased to 2
g_object_ref_sink(widget); // floating = false, ref count remains at 2

g_object_unref(widget); // floating = false, ref count decreases to 1

//No further unrefs, hello leak!

I hope that explains your leak, be sure to read the article mentioned above.

golen

g_object_is_floating is an api call which can be used to find if gtk object is floating point reference counted or not. Here is a link to example code: http://wikistack.com/managing-gtk-memorygui-application-interview-question/

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