servo / font-kit

A cross-platform font loading library written in Rust
Apache License 2.0
685 stars 103 forks source link

Fix thread-local freetype library memory leak #214

Closed messense closed 1 year ago

messense commented 1 year ago

According to https://freetype.org/freetype2/docs/reference/ft2-library_setup.html#ft_library

Library objects are normally created by FT_Init_FreeType, and destroyed with FT_Done_FreeType.

Currently it leaks the thread-local FT_Libaray memory:

==38981== 1,544 (400 direct, 1,144 indirect) bytes in 1 blocks are definitely lost in loss record 78 of 94
==38981==    at 0x4875058: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-arm64-linux.so)
==38981==    by 0x2B840F: ft_alloc (ftsystem.c:76)
==38981==    by 0x2B151B: ft_mem_qalloc (ftutil.c:75)
==38981==    by 0x2B1463: ft_mem_alloc (ftutil.c:54)
==38981==    by 0x2AC9FF: FT_New_Library (ftobjs.c:5496)
==38981==    by 0x2B37F3: FT_Init_FreeType (ftinit.c:224)
==38981==    by 0x1AECD7: font_kit::loaders::freetype::FREETYPE_LIBRARY::__init (freetype.rs:100)
==38981==    by 0x1AEE17: font_kit::loaders::freetype::FREETYPE_LIBRARY::__getit::{{closure}} (fast_local.rs:102)
==38981==    by 0x1AEEA7: std::sys::common::thread_local::lazy::LazyKeyInner<T>::initialize (mod.rs:54)
==38981==    by 0x1B40E3: std::sys::common::thread_local::fast_local::Key<T>::try_initialize (fast_local.rs:194)
==38981==    by 0x1B428F: std::sys::common::thread_local::fast_local::Key<T>::get (fast_local.rs:177)
==38981==    by 0x1AEDAB: font_kit::loaders::freetype::FREETYPE_LIBRARY::__getit (fast_local.rs:94)
jayvdb commented 10 months ago

Please see https://github.com/servo/font-kit/issues/224