集成测试的时候发现一个程序在持续的运行过程中有缓慢的内存增长。用ValGrind来查并没有太多的发现。询问开发人员的时候,他说GTK的内存好象都有一些泄露,网上有文曰
If GtkFoo isn't a toplevel window, then
foo = gtk_foo_new ();
gtk_widget_destroy (foo);
is a memory leak。
这个我是不相信的,于是有了两个内存泄露的问题需要解释。
1、我们的程序为什么内存泄露?在放弃了工具帮忙后,逐段代码注释,注释后并细心观察内存半个小时后记录内存起伏情况。最后发现我们的代码确实有问题。
GList *list_child;
......
list_child=gtk_container_get_children (GTK_CONTAINER (button));
fixed=GTK_WIDGET(list_child->data);
list_child=gtk_container_get_children (GTK_CONTAINER (fixed));
image=GTK_WIDGET(list_child->data);
shadow=button_array_item->shadow;
我们想当然认为list_child不用释放,是容器内部管理的GList列表,实际上,文档明确说返回值是一个newly-allocated的list :
gtk_container_get_children ()
GList
* gtk_container_get_children (GtkContainer
*container);
Returns the container's non-internal children. See gtk_container_forall() for details on what constitutes an "internal" child.
container :
a GtkContainer
Returns :
a newly-allocated list of the container's non-internal children.
2、同事说的例子为什么有内存泄露?找了文章看到下面的解释
1.5.
Why does my program leak memory, if I destroy a widget immediately after creating it ?
If GtkFoo isn't a toplevel window, then
foo = gtk_foo_new ();
gtk_widget_destroy (foo);
is a memory leak, because no one assumed the initial floating reference. If you are using a widget and you aren't immediately packing it into a container, then you probably want standard reference counting, not floating reference counting.
To to get this, you must acquire a reference to the widget and drop the floating reference (“ref and sink ” in GTK+ parlance) after creating it:
foo = gtk_foo_new ();
g_object_ref (foo);
gtk_object_sink (GTK_OBJECT (foo));
When you want to get rid of the widget, you must call gtk_widget_destroy() to break any external connections to the widget before dropping your reference:
gtk_widget_destroy (foo);
g_object_unref (foo);
When you immediately add a widget to a container, it takes care of assuming the initial floating reference and you don't have to worry about reference counting at all ... just call gtk_widget_destroy() to get rid of the widget.
分享到:
相关推荐
一个五脏俱全的Gtk小程序.txt 一个五脏俱全的Gtk小程序.txt
gtk 程序开发 编程 linux 学习linux gtk的朋友可以看一下
GTK第一个窗口程序。
GTK+程序设计中文版 .pdf
GTK+程序设计中文版
用Gtk+编程实现一个应用程序窗口,窗口中应包含: 一串文本,如“这是[xxx]的第一个Gtk+程序”。 两个按钮,一个按钮的标题为“确定”,另一个为“取消”。 设置窗口的标题为“Gtk+应用程序”。 用gtk_window_set_...
以下是一个简单的例子,展示了如何在一个基本的GTK应用程序中使用`GtkBox`来垂直排列两个按钮。确保安装了GTK(对于GTK 3或GTK 4,视所使用的版本而定)。例子使用GTK 3。 编译这个程序。在终端中,使用以下命令...
因为,一开始GTK+是用来编写“GIMP” –这个图形处理软件的。 从那以后, GTK+就逐渐成为了Linux and BSDUnix下最受欢迎的用于图形交互界面(GUI)设计的工具包之一。时至今日,大多数的Linux下的GUI软件(有图形交互...
特别是被GNOME选中使得GTK+广为流传,成为Linux下开发图形界面的应用程序的主流开发工具之一,当然GTK+并不要求必须在Linux上,事实上,目前GTK+已经有了成功的windows版本. GTK+ 是一种图形用户界面(GUI)工具...
基于gtk编写的计算器程序,发现gtk也挺强大的
GTK+ 简介:本 GTK+程序初级教程采用了“引导”式的方法来教你如何使用 GTK+。本教程所用的编程语言 为 C 语言。并且整个教程中方法已经在 Linux 中测试通过。本教程适用 GTK+的初级兼代中级 水平的程序员。
最后通过两个综合实例来巩固所学知识。本书实例丰富,理论联系实际,是一本实践性很强的编程参考书。本书虽然适合GTK+和GNOME编程的初学者学习,但不管是程序设计新手还是编程专家,都能从本书中获益。
GTK+程序设计中文版PDF
一个用GTK+ 写的图片放大缩小程序,仅供参考!!!!!!!!!!!!!!!!!!!!!!!!
最后通过两个综合实例来巩固所学知识。本书实例丰富,理论联系实际,是一本实践性很强的编程参考书。本书虽然适合GTK+和GNOME编程的初学者学习,但不管是程序设计新手还是编程专家,都能从本书中获益。
2.7 我安装一个 GTK+ 应用程序的时候, configure 抱怨说她找不到 GTK. 3. GTK+ 的目前进展状况 3.1 大家在讨论的 CVS 到底是个什么家伙,我怎么访问她呢? 3.2 我可以为 GTK+ 做些什么呢? 3.3 我怎么知道我的补丁...
一个gtk图形开发实例,可以实现一个简单的gtk实例程序
网上大部分arm-linnux-gcc编译gtk,其实介绍的是directfb+gtk移植。我提供的是gtk 应用程序的arm-linux-gcc编译方法。
本手册分为两个部分。第一部分是快速指南。 这部分将帮助你从头开始使用 PHP-GTK 2 编程,同时将深入讨论使用 PHP-GTK 2 设计程序的不同方法。另一部分是 指导手册。 手册的这个部分详细描述了所有 GTK 对象以及相关...
GTK程序设计源代码 《GTK+2.0编程范例》书本中的前两章源代码