我有问题泄漏在盘古你好世界的例子。
#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项目的懒惰自由策略再次出现?
最佳答案
我可以从
==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的类型系统得到设置据我所知,要消除这些假阳性并没有多少办法。
关于c - Pango + Cairo Hello World 泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37251165/