Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible memory leak in the Cairo library #1

Open
kdilayer opened this issue Jun 1, 2024 · 3 comments
Open

Possible memory leak in the Cairo library #1

kdilayer opened this issue Jun 1, 2024 · 3 comments

Comments

@kdilayer
Copy link

kdilayer commented Jun 1, 2024

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
Copy link
Contributor

sammycage commented Jun 1, 2024

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
Copy link
Author

kdilayer commented Jun 3, 2024

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

@sammycage sammycage changed the title Possible memory leak Possible memory leak in the Cairo library Jun 3, 2024
@sammycage
Copy link
Contributor

Thank you for your input! Since I am not the developer of cairo, you may consider reporting this to the cairo issue tracker for further evaluation.

https://gitlab.freedesktop.org/cairo/cairo/-/issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants