免费注册 查看新帖 |

Chinaunix

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

请教 g_object_unref(tilepixbuf)内存却没释放,晕ing [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-27 21:20 |只看该作者 |倒序浏览
#define MINWIDTH 320
#define MINHEIGHT 200


GtkWidget *board =0;
GdkPixbuf * tilepixbuf = NULL;
GtkWidget *mainWin =0;

static void expose_board (GtkWidget *w, GdkEventExpose *e, gpointer data)
{

          int width = gdk_pixbuf_get_width (tilepixbuf);
        int height = gdk_pixbuf_get_height (tilepixbuf);
        gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
                0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);

//        gtk_widget_queue_draw (board);


}
static void configure_board (GtkWidget *w, GdkEventConfigure *e, gpointer data)
{


        if(board->allocation.width <10 || board->allocation.height <10)
                return;

        printf("tilepixbuf->ref_count =%d\n",((GObject*)tilepixbuf)->ref_count);
        //while(((GObject*)tilepixbuf)->ref_count > 0)
        //        g_object_unref(tilepixbuf);
        g_object_unref(tilepixbuf);//这里想释放tilepixbuf
        printf("tilepixbuf->ref_count =%d\n",((GObject*)tilepixbuf)->ref_count);

        tilepixbuf = gdk_pixbuf_new_from_file_at_size("postmodern.svg",board->allocation.width,board->allocation.height,NULL);

        int width = gdk_pixbuf_get_width (tilepixbuf);
        int height = gdk_pixbuf_get_height (tilepixbuf);


        gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
                0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);

        gtk_widget_queue_draw (board);


        printf("configure_board , width =%d,height =%d\n",board->allocation.width,board->allocation.height);

       
       
}


gboolean load_images (gchar * file)
{
        mainWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);

        gtk_window_set_title (GTK_WINDOW (mainWin), "test");
        gtk_window_set_default_size (GTK_WINDOW (mainWin), 800, 600);

        board = gtk_drawing_area_new ();
        gtk_widget_show(board);
        gtk_container_add(mainWin,board);
        gtk_widget_show(mainWin);


int width =0;
int height =0;

        GError ge;
        memset(&ge,0,sizeof(GError));
        tilepixbuf = gdk_pixbuf_new_from_file_at_size(file,board->allocation.width,board->allocation.height,&ge);


        printf("tilepixbuf->ref_count =%d,GError=%s\n",((GObject*)tilepixbuf)->ref_count,ge.message);//这里奇怪,引用计数远大于1

//  tilepixbuf = gdk_pixbuf_new_from_file (file, NULL);

  //recreate_tile_images ();
//  gtk_widget_set_size_request (board, MINWIDTH, MINHEIGHT);

          width = gdk_pixbuf_get_width (tilepixbuf);
        height = gdk_pixbuf_get_height (tilepixbuf);

        gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
                0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);


//  gtk_widget_add_events (board, GDK_BUTTON_PRESS_MASK);
  
  g_signal_connect (G_OBJECT (board), "expose_event",
                    G_CALLBACK (expose_board), NULL);
  g_signal_connect (G_OBJECT (board), "configure_event",
                    G_CALLBACK (configure_board), NULL);
//  g_signal_connect (G_OBJECT (board), "button_press_event",
//                    G_CALLBACK (board_click), NULL);

  return TRUE;
}

int main(int argc,char** argv)
{
//        gtk_set_locale ();
        gtk_init (&argc, &argv);

//        if(!g_thread_supported())
//        {
//                g_thread_init(NULL);
//        }

        load_images("ppp.svg");

//        gdk_threads_enter();
        gtk_main();
//        gdk_threads_leave();
       
        return 0;
}

论坛徽章:
0
2 [报告]
发表于 2007-03-27 23:39 |只看该作者
GError 的处理不正确,可能由此造成了程序的无定义行为。参考 GError 的文档把这一部分处理正确之后再试试看。

论坛徽章:
0
3 [报告]
发表于 2007-03-28 23:12 |只看该作者
谢谢版主,GError是用错了,只是之前就是没加GError的情况,也是内存泄漏的,很严重,是不是svg库的问题呢

附件是我用的svg

postmodern.svg.bz2

36 KB, 下载次数: 72

论坛徽章:
0
4 [报告]
发表于 2007-03-28 23:18 |只看该作者
#include <gdk/gdkx.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>



#define MINWIDTH 320
#define MINHEIGHT 200


GtkWidget *board =0;
GdkPixbuf * tilepixbuf = NULL;
GtkWidget *mainWin =0;

static void expose_board (GtkWidget *w, GdkEventExpose *e, gpointer data)
{

        int width = gdk_pixbuf_get_width (tilepixbuf);
        int height = gdk_pixbuf_get_height (tilepixbuf);
        gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
                        0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);

}
static void configure_board (GtkWidget *w, GdkEventConfigure *e, gpointer data)
{
        if(board->allocation.width <10 || board->allocation.height <10)
                return;

        printf("tilepixbuf->ref_count =%d\n",((GObject*)tilepixbuf)->ref_count);
        //while(((GObject*)tilepixbuf)->ref_count > 0)
        //        g_object_unref(tilepixbuf);
        g_object_unref(tilepixbuf);//这里想释放tilepixbuf
        printf("tilepixbuf->ref_count =%d\n",((GObject*)tilepixbuf)->ref_count);

        tilepixbuf = gdk_pixbuf_new_from_file_at_size("postmodern.svg",board->allocation.width,board->allocation.height,NULL);

        int width = gdk_pixbuf_get_width (tilepixbuf);
        int height = gdk_pixbuf_get_height (tilepixbuf);


        gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
                        0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);

        gtk_widget_queue_draw (board);

        printf("configure_board , width =%d,height =%d\n",board->allocation.width,board->allocation.height);

}


int load_images (gchar * file)
{
        mainWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);

        gtk_window_set_title (GTK_WINDOW (mainWin), "test");
        gtk_window_set_default_size (GTK_WINDOW (mainWin), 800, 600);

        board = gtk_drawing_area_new ();
        gtk_widget_show(board);
        gtk_container_add(mainWin,board);
        gtk_widget_show(mainWin);


        int width =0;
        int height =0;

        tilepixbuf = gdk_pixbuf_new_from_file_at_size(file,board->allocation.width,board->allocation.height,NULL);
        printf("tilepixbuf->ref_count =%d\n",((GObject*)tilepixbuf)->ref_count);//这里奇怪,引用计数远大于1

        width = gdk_pixbuf_get_width (tilepixbuf);
        height = gdk_pixbuf_get_height (tilepixbuf);

        gdk_draw_pixbuf(board->window,board->style->fg_gc[GTK_STATE_NORMAL],tilepixbuf,
                        0,0,0,0,width,height,GDK_RGB_DITHER_NORMAL,0,0);


        g_signal_connect (G_OBJECT (board), "expose_event",
                        G_CALLBACK (expose_board), NULL);
        g_signal_connect (G_OBJECT (board), "configure_event",
                        G_CALLBACK (configure_board), NULL);

        return TRUE;
}

int main(int argc,char** argv)
{
        gtk_init (&argc, &argv);

        //        if(!g_thread_supported())
        //        {
        //                g_thread_init(NULL);
        //        }

        load_images("postmodern.svg");

        //        gdk_threads_enter();
        gtk_main();
        //        gdk_threads_leave();

        return 0;
}

[ 本帖最后由 linux_________ 于 2007-3-28 23:20 编辑 ]

Makefile

310 Bytes, 下载次数: 75

论坛徽章:
0
5 [报告]
发表于 2007-03-31 14:24 |只看该作者
请兄弟们再帮看看

main.c.bz2

1.08 KB, 下载次数: 54

论坛徽章:
0
6 [报告]
发表于 2007-04-01 21:29 |只看该作者
是不是因为没有为 GTK+ 安装 svg loader?还有,你应该在程序中对 gdk_pixbuf_new_from_file_at_size() 的返回值进行检查。

论坛徽章:
0
7 [报告]
发表于 2007-04-01 21:55 |只看该作者
回版主,上面的程序显示出来正确的图像,也可以按预期的目标,实现变大变小,
如果gdk_pixbuf_new_from_file_at_size返回NULL,程序应该马上就挂了,所以太奇怪了

论坛徽章:
0
8 [报告]
发表于 2007-04-01 22:00 |只看该作者
运行时有这个错误
~# ./test
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
art_render_invoke: no image source given
tilepixbuf->ref_count =75

论坛徽章:
0
9 [报告]
发表于 2007-04-01 22:30 |只看该作者
是不是 svg 图像本身的问题?另外找一个简单的 svg 图像或者换一幅其它类型的图像(如 jpg)试试看。

论坛徽章:
0
10 [报告]
发表于 2007-04-04 21:47 |只看该作者
换个好几个SVG图都是一样,都对gtk快绝望了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP