Giter Club home page Giter Club logo

Comments (7)

springmeyer avatar springmeyer commented on June 15, 2024

@lbud - here's what a scope guard could look like:

diff --git a/src/glyphs.cpp b/src/glyphs.cpp
index 55cca87..a1d6c7b 100644
--- a/src/glyphs.cpp
+++ b/src/glyphs.cpp
@@ -168,10 +168,24 @@ NAN_METHOD(Range) {
     NanReturnUndefined();
 }

+struct ft_library_guard
+{
+    ft_library_guard(FT_Library lib) :
+      library_(lib) {}
+
+    ~ft_library_guard()
+    {
+        if (library_) FT_Done_FreeType(library_);
+    }
+
+    FT_Library library_;
+};
+
 void LoadAsync(uv_work_t* req) {
     LoadBaton* baton = static_cast<LoadBaton*>(req->data);

     FT_Library library = nullptr;
+    ft_library_guard library_guard(library);
     FT_Error error = FT_Init_FreeType(&library);
     if (error) {
         /* LCOV_EXCL_START */
@@ -205,7 +219,6 @@ void LoadAsync(uv_work_t* req) {
             FT_Done_Face(ft_face);
         }
     }
-    FT_Done_FreeType(library);

Want to run this out and see if you can replicate the memory leaks and try to fix them?

Note: iprofiler -leaks might also be able to find them, but as you saw on Friday my Instruments.app is broken (crashes when I try to open it), so I was unable to confirm that iprofiler sampling could pick up the leaks. I would recommend you learn both iprofiler/instruments.app and valgrind as you have time, but overall put more trust in valgrind on linux (sadly it does not run on recent OS X).

from node-fontnik.

lbud avatar lbud commented on June 15, 2024

@springmeyer fwiw this is all I've been able to get out of Instruments so far:
image
That's only profiling fontnik.range. Profiling fontnik.load shows no leaks in Instruments. (So I may be doing it wrong but iprofiler does seem to be less effective as valgrind.)

from node-fontnik.

springmeyer avatar springmeyer commented on June 15, 2024

Okay, good at least that it caught one of two. I wonder if iprofiler/instruments could catch the FT_Library leak if you ran this test in a loop for a few seconds. I think iprofiler works by sampling on intervals so it may be that the time between the function starting and the leak happening is so short that it is missed. Valgrind instruments and tracks all allocations so it should not miss anything, but is also very very slow (sometimes prohibitively slow).

from node-fontnik.

lbud avatar lbud commented on June 15, 2024

Hm, I tried changing bench.js to run invalid buffers or running iprofiler on the tests, but it always either hangs in saving results or doesn't show leaks. But I brew installed valgrind and was able to reproduce.

1b408bd was my attempt to fix these, but I'm having a few issues. Full valgrind output is here.

  • There are a lot of issues around the uninitialized value here, which i also get a compiler warning about:
../src/glyphs.cpp:545:32: warning: variable 'ft_glyph' is uninitialized when used here [-Wuninitialized]
    ft_glyph_guard glyph_guard(ft_glyph);
                               ^~~~~~~~
../src/glyphs.cpp:544:22: note: initialize the variable 'ft_glyph' to silence this warning
    FT_Glyph ft_glyph;
                     ^
                      = nullptr

Is there any way I can initialize that or is this just an inevitable error because of the FT library?

  • I'm still getting
==38475== 22,224 (392 direct, 21,832 indirect) bytes in 1 blocks are definitely lost in loss record 1,020 of 1,037
==38475==    at 0x1007BA59B: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==38475==    by 0x10A16670D: ft_mem_alloc (in /usr/local/Cellar/freetype/2.5.5/lib/libfreetype.6.dylib)
==38475==    by 0x10A16A648: FT_New_Library (in /usr/local/Cellar/freetype/2.5.5/lib/libfreetype.6.dylib)
==38475==    by 0x10A1654EF: FT_Init_FreeType (in /usr/local/Cellar/freetype/2.5.5/lib/libfreetype.6.dylib)
==38475==    by 0x10A001722: node_fontnik::LoadAsync(uv_work_s*) (in /Users/laurenbudorick/src/node-fontnik/lib/fontnik.node)
==38475==    by 0x100139148: worker (in /usr/local/bin/node)
==38475==    by 0x10012F935: uv__thread_start (in /usr/local/bin/node)
==38475==    by 0x1034E42FB: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==38475==    by 0x1034E4278: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==38475==    by 0x1034E24B0: thread_start (in /usr/lib/system/libsystem_pthread.dylib)
==38475== 
==38475== 44,448 (128 direct, 44,320 indirect) bytes in 2 blocks are definitely lost in loss record 1,028 of 1,037
==38475==    at 0x1007BA59B: malloc (in /usr/local/Cellar/valgrind/HEAD/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==38475==    by 0x10A16670D: ft_mem_alloc (in /usr/local/Cellar/freetype/2.5.5/lib/libfreetype.6.dylib)
==38475==    by 0x10A169FAE: FT_Add_Module (in /usr/local/Cellar/freetype/2.5.5/lib/libfreetype.6.dylib)
==38475==    by 0x10A16551D: FT_Init_FreeType (in /usr/local/Cellar/freetype/2.5.5/lib/libfreetype.6.dylib)
==38475==    by 0x10A001722: node_fontnik::LoadAsync(uv_work_s*) (in /Users/laurenbudorick/src/node-fontnik/lib/fontnik.node)
==38475==    by 0x100139148: worker (in /usr/local/bin/node)
==38475==    by 0x10012F935: uv__thread_start (in /usr/local/bin/node)
==38475==    by 0x1034E42FB: _pthread_body (in /usr/lib/system/libsystem_pthread.dylib)
==38475==    by 0x1034E4278: _pthread_start (in /usr/lib/system/libsystem_pthread.dylib)
==38475==    by 0x1034E24B0: thread_start (in /usr/lib/system/libsystem_pthread.dylib)

😕

from node-fontnik.

springmeyer avatar springmeyer commented on June 15, 2024

Ah, okay try doing:

+FT_Glyph ft_glyph = nullptr;
ft_glyph_guard glyph_guard(ft_glyph);

To make sure the ft_glyph is initialized to something. We are already doing this for the library.

from node-fontnik.

springmeyer avatar springmeyer commented on June 15, 2024

running iprofiler on the tests, but it always either hangs in saving results or doesn't show leaks

Yes, i've seen that iprofiler generates such a large amount of metadata that will may hang or crash for tests runs that are long. So, its most viable for short test runs (like test/test.js rather than bench/bench.js) which of course is lame if it cannot catch the leaks. Great that you figured out how to get valgrind going on OS X - I just followed your lead and installed it and am testing.

from node-fontnik.

springmeyer avatar springmeyer commented on June 15, 2024

done in #83

from node-fontnik.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.