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

Author Topic: Crash on exit with fonts  (Read 1895 times)

0 Members and 1 Guest are viewing this topic.

Gez

  • Newbie
  • *
  • Posts: 4
    • View Profile
Crash on exit with fonts
« on: March 11, 2013, 10:00:28 pm »
Hello,

I'm using SFML f5b9c7e2160e758f32aa6453c9f6b001fabbcca8 straight from GitHub. I have compiled it aswith MS VC++ 2005 Express Edition, using the project files generated by CMake. (BTW, this isn't supported by VC++ 2005. Not that it really matters I guess.)

It is statically linked to this program, a generic archive/resource/map editor for Doom engine games. The problem is what happens when the SFML code is called to render some text. When quitting afterwards, the program crashes, with exception c0000005 (access violation).  I have tried debug, release, and minsizerel builds, with no difference in outcome.

Important note: this doesn't seem to happen when compiled with Visual Studio 2010 or 2012, as the other developer working on the same project doesn't run into the issue anymore, though he did with the RC version.

What happens is that there are several global sf::Font instances that are created when the application is started. Much later, at the user's input, they might be used to draw text, on a RenderWindow that is created when needed. This happens notably when using the map editor part of the app.

I have found out that if instead of setting text directly to one of the global fonts, I instead create a clone of it with the copy constructor, then I do not get the appcrash on exit. However, this hurts performances, since the font is copied every time text is drawn. (And it is horribly ugly code anyway.)

I have no idea what to do. Looking at the closed ticket and related thread, it seems I'm getting a bug that has been fixed for other people. Is it VC++ 2005's fault? Or is there an obscure platform-version conditional somewhere in SFML that creates this difference?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Crash on exit with fonts
« Reply #1 on: March 11, 2013, 11:03:42 pm »
Quote
BTW, this isn't supported by VC++ 2005. Not that it really matters I guess
It's not Visual C++ 2005, it's the Express edition (it's the same with VC++ 2008 Express and VS 2012 Express).

Quote
What happens is that there are several global sf::Font instances
Don't use global font instances, their destructor is not trivial and involves internal resources. Since SFML uses globals too, and the order of destruction of globals is undefined across translation units, this may explain your bug (and the fact that it can work fine for others).
Laurent Gomila - SFML developer

Gez

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Crash on exit with fonts
« Reply #2 on: March 12, 2013, 09:56:44 pm »
Don't use global font instances, their destructor is not trivial and involves internal resources.
Thanks, I guess a FontManager class to encapsulate those fonts is more elegant anyway and it works around the issue nicely.