plutoprint / plutobook

Paged HTML Rendering Library
MIT License
13 stars 2 forks source link

Possible memory leak in the Cairo library #1

Open kdilayer opened 1 month ago

kdilayer commented 1 month ago

This is an awsome library!

There is a possible memory leak ... but I cannot figure out if it is related to cairo or bluebook. It is related to rendering text on pdf surfaces (fonts and glyphs).

valgrind --leak-check=yes --track-origins=yes --leak-check=full --show-leak-kinds=all --log-file="valgrind.log" -s ./myapp

** case when writeToPdf = false;

==4569== LEAK SUMMARY: ==4569== definitely lost: 0 bytes in 0 blocks ==4569== indirectly lost: 0 bytes in 0 blocks ==4569== possibly lost: 0 bytes in 0 blocks ==4569== still reachable: 381,786 bytes in 148 blocks ==4569== suppressed: 0 bytes in 0 blocks ==4569== ==4569== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

** case when writeToPdf = true; ( ==> lost 9200 bytes !! ) ==5352== LEAK SUMMARY: ==5352== definitely lost: 9,200 bytes in 200 blocks ==5352== indirectly lost: 0 bytes in 0 blocks ==5352== possibly lost: 0 bytes in 0 blocks ==5352== still reachable: 129,078 bytes in 151 blocks ==5352== suppressed: 0 bytes in 0 blocks ==5352== ==5352== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

To reproduce:

  bool writeToPdf = true;
  for(int i=0; i < 100; i++)      {
      plutobook::Book book(plutobook::PageSize::A4, plutobook::PageMargins::Normal);
      book.loadHtml("a");

      if(writeToPdf) {
          book.writeToPdf("hello.pdf"); 
      }
      else {
          book.writeToPng("hello.png"); 
      }
  }
  return 1;
sammycage commented 1 month ago

Thanks for testing the library and providing detailed feedback. The memory leak you are encountering is related to cairo, not plutobook. See Issue: https://gitlab.freedesktop.org/cairo/cairo/-/issues/805

==12984== 138 bytes in 3 blocks are definitely lost in loss record 98 of 195
==12984==    at 0x4E050C5: malloc (vg_replace_malloc.c:442)
==12984==    by 0x58F358E: strdup (strdup.c:42)
==12984==    by 0x5B7D695: cairo_font_options_merge (cairo-font-options.c:305)
==12984==    by 0x5BEE148: _cairo_surface_wrapper_show_text_glyphs (cairo-surface-wrapper.c:440)
==12984==    by 0x5BD059E: _cairo_recording_surface_replay_internal (cairo-recording-surface.c:2280)
==12984==    by 0x5BD0FE2: _cairo_recording_surface_replay_and_create_regions (cairo-recording-surface.c:2575)
==12984==    by 0x5BA2854: _paint_page (cairo-paginated-surface.c:431)
==12984==    by 0x5BA2DC7: _cairo_paginated_surface_show_page (cairo-paginated-surface.c:602)
==12984==    by 0x5BF2A16: cairo_surface_show_page (cairo-surface.c:2534)
==12984==    by 0x5B8201E: _cairo_gstate_show_page (cairo-gstate.c:1440)
==12984==    by 0x5B7A17F: _cairo_default_context_show_page (cairo-default-context.c:1206)
==12984==    by 0x5C08302: cairo_show_page (cairo.c:2506)
==12984==
==12984== 138 bytes in 3 blocks are definitely lost in loss record 99 of 195
==12984==    at 0x4E050C5: malloc (vg_replace_malloc.c:442)
==12984==    by 0x58F358E: strdup (strdup.c:42)
==12984==    by 0x5B7D695: cairo_font_options_merge (cairo-font-options.c:305)
==12984==    by 0x5BEE148: _cairo_surface_wrapper_show_text_glyphs (cairo-surface-wrapper.c:440)
==12984==    by 0x5BD059E: _cairo_recording_surface_replay_internal (cairo-recording-surface.c:2280)
==12984==    by 0x5BD1082: _cairo_recording_surface_replay_region (cairo-recording-surface.c:2598)
==12984==    by 0x5BA2A3B: _paint_page (cairo-paginated-surface.c:484)
==12984==    by 0x5BA2DC7: _cairo_paginated_surface_show_page (cairo-paginated-surface.c:602)
==12984==    by 0x5BF2A16: cairo_surface_show_page (cairo-surface.c:2534)
==12984==    by 0x5B8201E: _cairo_gstate_show_page (cairo-gstate.c:1440)
==12984==    by 0x5B7A17F: _cairo_default_context_show_page (cairo-default-context.c:1206)
==12984==    by 0x5C08302: cairo_show_page (cairo.c:2506)
kdilayer commented 1 month ago

Here is a patch for cairo 1.18 to fix the memory leak - I have no idea if it is a good fix or not but the library works and no memory leaks reported:

cairo-patch.txt