Skip to content

Fix memory leaks in overhaul branch #82

@springmeyer

Description

@springmeyer

The overhaul branch no longer uses C++ wrappers around Freetype objects. @lbud one side effect of this is that we lost the RAII benefit of those classes.

To solve this I propose we use scope guards (http://en.wikibooks.org/wiki/More_C++_Idioms/Scope_Guard). Scope gaurds make sense because in our used of FT objects is now scoped purely to the async methods. Scope guards will help use ensure that the Freetype objects get destructed the same no matter if the function throws an error or returns early.

Here are the current leaks:

For reference I found the memory leaks by:

  • booting a linux VM (since OS X does not support valgrind and iprofiler -leaks is not as good as valgrind)
  • built overhaul branch of node-fontnik in debug mode
  • ran valgrind --leak-check=full node ./node_modules/.bin/_mocha test/
  • paid attention to the sections that originated from node-fontnik function calls and said blocks are definitely lost and ignored all others

Valgrind results that showed the memory leaks were:

==5575== 320 bytes in 4 blocks are definitely lost in loss record 230 of 309
==5575==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5575==    by 0x7B5CC6F: ft_mem_qalloc (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x7B5CCB7: ft_mem_alloc (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x7B63177: ??? (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x7B634CE: FT_Get_Glyph (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x78C7FC5: node_fontnik::RenderSDF(node_fontnik::glyph_info&, int, int, float, FT_FaceRec_*) (glyphs.cpp:522)
==5575==    by 0x78C69B7: node_fontnik::RangeAsync(uv_work_s*) (glyphs.cpp:290)
==5575==    by 0x9E0E2F: worker (threadpool.c:74)
==5575==    by 0x9D6640: uv__thread_start (uv-common.c:323)
==5575==    by 0x5A6B181: start_thread (pthread_create.c:312)
==5575==    by 0x5D7BEFC: clone (clone.S:111)

And

==5575== 2,128 (128 direct, 2,000 indirect) bytes in 1 blocks are definitely lost in loss record 273 of 309
==5575==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5575==    by 0x7B5CC6F: ft_mem_qalloc (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x7B5CCB7: ft_mem_alloc (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x7B61431: FT_Add_Module (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x7B5856B: FT_Add_Default_Modules (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x7B585C8: FT_Init_FreeType (in /usr/lib/x86_64-linux-gnu/libfreetype.so.6.11.1)
==5575==    by 0x78C5D40: node_fontnik::LoadAsync(uv_work_s*) (glyphs.cpp:175)
==5575==    by 0x9E0E2F: worker (threadpool.c:74)
==5575==    by 0x9D6640: uv__thread_start (uv-common.c:323)
==5575==    by 0x5A6B181: start_thread (pthread_create.c:312)
==5575==    by 0x5D7BEFC: clone (clone.S:111)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions