pango + cairo hello world leaks

时间:2016-05-16 09:55:47

标签: memory-leaks glib cairo pango

我在pangocairo hello world示例中遇到泄漏问题。

#include <cairo.h>
#include <pango/pangocairo.h>

int main(int argc, char *argv[])
    {
//  0
    cairo_surface_t* surface=cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
//  1
    cairo_t* cr=cairo_create(surface);

//  2
    PangoFontDescription* font_description=pango_font_description_new();
    pango_font_description_set_family(font_description,"serif");
    pango_font_description_set_weight(font_description,PANGO_WEIGHT_BOLD);
    pango_font_description_set_absolute_size(font_description,32*PANGO_SCALE);

//  3
    PangoLayout* layout=pango_cairo_create_layout(cr);

    pango_layout_set_font_description(layout,font_description);
    pango_layout_set_text(layout,"Hello, world",-1);
    cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
    cairo_move_to (cr, 10.0, 50.0);
    pango_cairo_show_layout(cr,layout);

//  3
    g_object_unref(layout);

//  2
    pango_font_description_free(font_description);

//  1
    cairo_destroy (cr);

    cairo_surface_write_to_png (surface, "hello.png");

//  0
    cairo_surface_destroy (surface);
    return 0;
    }

Valgrind输出

==5178==    definitely lost: 7,936 bytes in 28 blocks
==5178==    indirectly lost: 8,510 bytes in 374 blocks
==5178==      possibly lost: 1,514 bytes in 21 blocks
==5178==    still reachable: 567,835 bytes in 5,069 block

抱怨libfontconfig和glib。上面有没有丢失的清理工作?如果不是我应该关心它(发布错误报告),还是GNOME项目的懒惰免费政策再次出现?

1 个答案:

答案 0 :(得分:1)

我可以从

那里得到
==8376==    definitely lost: 3,840 bytes in 12 blocks
==8376==    indirectly lost: 7,822 bytes in 339 blocks
==8376==      possibly lost: 1,514 bytes in 21 blocks
==8376==    still reachable: 573,848 bytes in 4,977 blocks

==8394==    definitely lost: 0 bytes in 0 blocks
==8394==    indirectly lost: 0 bytes in 0 blocks
==8394==      possibly lost: 1,514 bytes in 21 blocks
==8394==    still reachable: 60,064 bytes in 368 blocks

通过添加

    pango_cairo_font_map_set_default(NULL);
    cairo_debug_reset_static_data();
    FcFini();

在文件末尾。请注意,FcFini()需要额外的#include <fontconfig/fontconfig.h>并针对fontconfig进行链接。

快速浏览一下,valgrind的其余投诉来自GLib的类型系统的设置。据我所知,要摆脱那些假阳性没有什么可做的。