Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Memory Leak on Font::loadGlyph  (Read 2393 times)

0 Members and 2 Guests are viewing this topic.

cesarizu

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Memory Leak on Font::loadGlyph
« on: November 12, 2016, 05:57:20 am »
Hello,

I've been running a sample app through valgrind and I see that sf::Font::loadGlyph is leaking memory. According to https://lists.nongnu.org/archive/html/freetype-devel/2008-08/msg00014.html the call to FT_Glyph_To_Bitmap creates a new glyph that needs to be free'd with FT_Done_Glyph. Doing that, that particular leak is gone.

I have a patch ready to be submitted that fixes this leak, should I open a PR on github for that?

Thanks!
César

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Laurent Gomila - SFML developer

cesarizu

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Memory Leak on Font::loadGlyph
« Reply #2 on: November 12, 2016, 09:58:28 am »
The problem is that FT_Glyph_To_Bitmap at https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp#L559 changes the glyphDesc pointer to a new value, the original one is lost. The solution is to save a copy and free both at the end of the function.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Memory Leak on Font::loadGlyph
« Reply #3 on: November 12, 2016, 11:59:31 am »
Read carefully through the mailing list excerpt again and the corresponding code in SFML. In SFML's case, we are calling FT_Glyph_To_Bitmap with the destroy parameter set to 1, instructing FreeType to destroy the glyph that got replaced before returning if no error occurred. If FreeType does not do this in your case then it is bugged. There have been many valgrind passes over SFML in the past, and this kind of leak would have been obvious if it had been a problem.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

cesarizu

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Memory Leak on Font::loadGlyph
« Reply #4 on: November 12, 2016, 07:08:47 pm »
It might be a bug with that parameter in FT. In my tests I changed that to 0, leaving it at one didn't clear the memory, and trying to clear manually with 1 gave some other errors. The 1 there seems to free some resources but not all, that's the original concern of the mailing list post.

Running though valgrind is really clear there's a memory leak there. If you want, I can post a PR with some more detailed valgrind logs so you can see how it changes before and after.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Memory Leak on Font::loadGlyph
« Reply #5 on: November 12, 2016, 07:35:43 pm »
The mailing list clearly says that they call FT_Done_Glyph twice because they leave the "destroy" parameter of FT_Glyph_To_Bitmap to 0. The official documentation of the function is also very clear about the behaviour of this argument. And if you insist we can find other sources that confirm this. So I still see nothing wrong in what SFML does.

If you agree that SFML is using FreeType as it should, then maybe you should try to reproduce the leak in a complete and minimal code with only FreeType calls, and submit it to the FreeType mailing list. Make sure that your version of FreeType is up-to-date first ;)
Laurent Gomila - SFML developer

cesarizu

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Memory Leak on Font::loadGlyph
« Reply #6 on: November 13, 2016, 06:15:14 am »
You are right ::) FT is not working as expected, I'll create a sample app that replicates this leak and post it to the FT devel list. In any case it's not a critical leak as it only happens when loading fonts, which is normally just at the start of the app.

 

anything