免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3178 | 回复: 5
打印 上一主题 下一主题

为什么一个简单的gtk 程序会有这么多问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-06-04 19:32 |只看该作者 |倒序浏览
用valgrind 测试一下一个简单的gtk 程序,发现N 多问题

  1. #include <gtk/gtk.h>

  2. int main(int argc, char * argv[])
  3. {
  4.     /*-- Declare the GTK Widgets used in the program --*/
  5.     GtkWidget *window;

  6.     /*--  Initialize GTK --*/
  7.     gtk_init(&argc, &argv);

  8.     /*-- Create the new window --*/
  9.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  10.     /*-- Display the widgets --*/
  11.     gtk_widget_show(window);

  12.     /*-- Start the GTK event loop --*/
  13.     gtk_main();

  14.     /*-- Return 0 if exit is successful --*/
  15.     return 0;
  16. }
复制代码


编译选项

  1. gcc -g -o gtk main.c `pkg-config gtk+-2.0 --cflags --libs`
复制代码


在终端中运行后,显示GTK窗口,关闭后, 无法正常返回到终端,程序停在 gtk_main(); 里面不出来
只好用Ctrl + C 退出

用valgrind 检测,问题更多。
valgrind ./gtk后的结果, 因无法退出,中途用Ctrl +C 退出

==10423== ERROR SUMMARY: 13 errors from 10 contexts (suppressed: 83 from 1)
==10423== malloc/free: in use at exit: 211,888 bytes in 2,684 blocks.
==10423== malloc/free: 7,720 allocs, 5,036 frees, 747,250 bytes allocated.
==10423== For counts of detected errors, rerun with: -v
==10423== searching for pointers to 2,684 not-freed blocks.
==10423== checked 487,236 bytes.
==10423==
==10423== LEAK SUMMARY:
==10423==    definitely lost: 156 bytes in 11 blocks.
==10423==      possibly lost: 52,520 bytes in 51 blocks.
==10423==    still reachable: 159,212 bytes in 2,622 blocks.
==10423==         suppressed: 0 bytes in 0 blocks.
==10423== Use --leak-check=full to see details of leaked memory.


郁闷,还好用的是gtkmm,

下面是gtkmm的测试

  1. #include <gtkmm.h>

  2. int main(int argc, char *argv[])
  3. {
  4.     Gtk::Main kit(argc, argv);
  5.    
  6.     Gtk::Window window;

  7.     Gtk::Main::run(window);
  8.    
  9.     return 0;
  10. }
复制代码



问题也不少,不过可以正常返回到终端
==10546== ERROR SUMMARY: 13 errors from 11 contexts (suppressed: 101 from 1)
==10546== malloc/free: in use at exit: 285,393 bytes in 4,958 blocks.
==10546== malloc/free: 10,957 allocs, 5,999 frees, 852,248 bytes allocated.
==10546== For counts of detected errors, rerun with: -v
==10546== searching for pointers to 4,958 not-freed blocks.
==10546== checked 957,976 bytes.
==10546==
==10546== LEAK SUMMARY:
==10546==    definitely lost: 156 bytes in 11 blocks.
==10546==      possibly lost: 53,024 bytes in 52 blocks.
==10546==    still reachable: 232,213 bytes in 4,895 blocks.
==10546==         suppressed: 0 bytes in 0 blocks.
==10546== Use --leak-check=full to see details of leaked memory.


一个简单的GTK就有这么多问题,还真是不好用,郁闷...

论坛徽章:
0
2 [报告]
发表于 2007-06-04 21:20 |只看该作者
无论什么东西,你不正确使用它,就不要预期它有正确的行为.

我没用过gtkmm,但在前一段代码里面,你既没有对delete_event事件也没有对destroy事件进行处理,你写C程序时只进行malloc而不free当然会产生memory leak.你不从调用gtk_main_quit自然无法从循环中退出.

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2007-06-04 21:25 |只看该作者
原帖由 coldwarm 于 2007-6-4 21:20 发表
无论什么东西,你不正确使用它,就不要预期它有正确的行为.

此言甚是!

论坛徽章:
0
4 [报告]
发表于 2007-06-04 22:05 |只看该作者
谢谢2楼的回复, 第一个例子是从下面这个网址找到的
http://www.gtk.org/tutorial/c39.html
我确实没注意到前面的那句话
This program will create a 200x200 pixel window and has no way of exiting except to be killed by using the shell.


第二个例子是下面这个网址找到的
http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/ch03.html

目前是在学习gtkmm(没有学过GTK), 一开始就有问题,确实觉得很奇怪,就到GTK的网站上去找了一个GTK的例子试了一下,

发现有类似的问题,不知道问题出在那里,所以才发贴子问的.

改了一下,可以正常返回到终端了。


  1. #include <gtk/gtk.h>

  2. static gboolean delete_event( GtkWidget *widget,
  3.         GdkEvent  *event,
  4.         gpointer   data )
  5. {
  6.     g_print ("delete event occurred,exit window.\n");
  7.     gtk_main_quit();
  8.     return FALSE;
  9. }

  10. int main(int argc, char * argv[])
  11. {
  12.     /*-- Declare the GTK Widgets used in the program --*/
  13.     GtkWidget *window;

  14.     /*--  Initialize GTK --*/
  15.     gtk_init(&argc, &argv);

  16.     /*-- Create the new window --*/
  17.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  18.    
  19.     g_signal_connect (G_OBJECT (window), "delete_event",
  20.             G_CALLBACK (delete_event), NULL);

  21.     /*-- Display the widgets --*/
  22.     gtk_widget_show(window);

  23.     /*-- Start the GTK event loop --*/
  24.     gtk_main();

  25.     /*-- Return 0 if exit is successful --*/
  26.     return 0;
  27. }
复制代码

==17025== ERROR SUMMARY: 13 errors from 11 contexts (suppressed: 83 from 1)
==17025== malloc/free: in use at exit: 211,984 bytes in 2,688 blocks.
==17025== malloc/free: 7,773 allocs, 5,085 frees, 751,269 bytes allocated.
==17025== For counts of detected errors, rerun with: -v
==17025== searching for pointers to 2,688 not-freed blocks.
==17025== checked 487,584 bytes.
==17025==
==17025== LEAK SUMMARY:
==17025==    definitely lost: 156 bytes in 11 blocks.
==17025==      possibly lost: 52,520 bytes in 51 blocks.
==17025==    still reachable: 159,308 bytes in 2,626 blocks.
==17025==         suppressed: 0 bytes in 0 blocks.
==17025== Use --leak-check=full to see details of leaked memory.


实在是不知道该怎么改了,好像没有找到 gtk_window_delete

请指导一下,谢谢!

[ 本帖最后由 antonym55 于 2007-6-4 22:51 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2007-06-05 11:55 |只看该作者
一般都是依据deleve_event事件处理器返回TRUE还是FALSE来判断是否激发destroy事件,在destroy事件处理器中调用gtm_main_quit.

看一下这两个连接.取决于你的应用所使用的模块.

http://live.gnome.org/Valgrind
http://primates.ximian.com/~federico/news-2006-04.html

加上相应的选项运行valgrind.

论坛徽章:
0
6 [报告]
发表于 2007-06-06 09:46 |只看该作者
gtk很好用。g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
或g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_widget_destroy), NULL);
都行
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP