如何正确(和有效)释放gtk小部件中的内存 [英] How to correctly (and effectively) release memory in a gtk widget

查看:576
本文介绍了如何正确(和有效)释放gtk小部件中的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何在完成GTK小部件时正确释放内存,例如,如果我需要创建并销毁很多小部件。但是,无论我尝试什么,valgrind似乎都会指示内存泄漏。我查看了其他问题,其中包括一个列出了GTK的valgrind压缩文件的问题,但它没有改变结果。



这里是最简单的代码片段来重现我的问题:

  #includegtk / gtk.h

int main()
{
GtkWidget * widget = gtk_fixed_new();
g_object_ref(widget);
g_object_ref_sink(widget); //移除浮动引用,并自己拥有这个对象

g_object_unref(widget);

gtk_widget_destroy(widget);



$ b $ p
$ b

我的期望是(在处理浮动引用之后),unref()函数应该将参考计数减少到零,然后释放所有内存。我向其中扔了gtk_widget_destroy(),但我不确定它实际上是否有必要(并且它不会改变泄漏的大小)。

valgrind命令的输出 G_SLICE = debug-blocks valgrind ./t3 --supression =〜/ Downloads / GNOME.supp 来自问题 GTK hello_world程序中的内存泄漏


  == 10079 == HEAP SUMMARY:
== 10079 ==在退出时使用:在847块中使用164,338字节
== 10079 ==总堆使用情况:1,380个分配,533个释放,219,176个字节分配
== 10079 ==
== 10079 ==泄漏摘要:
== 10079 ==绝对丢失:0字节0块
== 10079 ==间接丢失:0块中的0字节
== 10079 ==可能丢失:174块中的21,350字节
== 10079 ==仍然可达:142,988字节673块
== 10079 ==抑制:0块中的0字节
== 10079 ==重新运行--leak-check = full查看泄漏内存的详细信息

我看过的其他文档是 http://www.demko.ca/blog /posts/200705_gtkmm_refcoutning.txt https://developer.gnome.org /gtk2/2.24/GtkObject.html



您可以使用

  gcc -std = gnu99`pkg-config --cflags gtk + -2.0` t3.c -o t3`pkg-config --libs gtk + -2.0 gthread-2.0` 

任何人都知道我错过了什么?是否还有另外一个函数需要调用以确保内存被释放?

解决方案

   - g_object_ref 

将ref计数增加一个

- g_object_unref

如果ref count == 0,则将ref计数减1,对象被销毁

- g_object_ref_sink

如果对象有一个浮动参考,它将该参考转换为一个正常参考(吸收它)
否则它将参考计数增加一个

- 所有对象的浮动引用计数为1

阅读,我建议你看看下面的文章: GTK +中的内存管理介绍



现在,看看您的示例,让我们看看函数调用及其功能: p>

  GtkWidget * widget = gtk_fixed_新(); //用ref计数1创建的小部件| floating = true 
g_object_ref(widget); // floating = true,ref count增加到2
g_object_ref_sink(widget); //浮动= false,ref count保持在2

g_object_unref(widget); // floating = false,ref count减少到1

//没有更多的unrefs,你好泄漏!

我希望能解释您的泄漏情况,请务必阅读上述文章。


I'm trying to understand how to correctly release memory when I am finished with a GTK widget, for example if I need to create and destroy many widgets. However, valgrind seems to indicate a memory leak regardless of what I try. I've looked at other questions, including one that lists a valgrind supression file for GTK, but it didn't change the result.

Here's the simplest code snippet to reproduce my issue:

#include "gtk/gtk.h"

int main()
{
    GtkWidget * widget = gtk_fixed_new();
    g_object_ref(widget);
    g_object_ref_sink(widget); // remove floating reference, and own this object ourselves

    g_object_unref(widget);

    gtk_widget_destroy(widget);
}

My expectation is that (after dealing with floating references), the unref() function should reduce the reference count to zero, and then all memory is released. I threw the gtk_widget_destroy() in there for good measure, but I'm not sure it should actually be necessary (and it doesn't change the magnitude of the leak).

The output with the valgrind command G_SLICE=debug-blocks valgrind ./t3 --supression=~/Downloads/GNOME.supp from question Memory Leaks in GTK hello_world program is

==10079== HEAP SUMMARY:
==10079==     in use at exit: 164,338 bytes in 847 blocks
==10079==   total heap usage: 1,380 allocs, 533 frees, 219,176 bytes allocated
==10079== 
==10079== LEAK SUMMARY:
==10079==    definitely lost: 0 bytes in 0 blocks
==10079==    indirectly lost: 0 bytes in 0 blocks
==10079==      possibly lost: 21,350 bytes in 174 blocks
==10079==    still reachable: 142,988 bytes in 673 blocks
==10079==         suppressed: 0 bytes in 0 blocks
==10079== Rerun with --leak-check=full to see details of leaked memory

The other documentation I've looked at is http://www.demko.ca/blog/posts/200705_gtkmm_refcoutning.txt and https://developer.gnome.org/gtk2/2.24/GtkObject.html

You can compile my snippet with

gcc -std=gnu99 `pkg-config --cflags gtk+-2.0` t3.c -o t3 `pkg-config --libs gtk+-2.0 gthread-2.0`

Anyone know what I'm missing? Is there another function I should be calling to ensure the memory is released?

解决方案

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

这篇关于如何正确(和有效)释放gtk小部件中的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆