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

Author Topic: Drawing sf::Text results in a crash"writing_system_class" was nullptr  (Read 9203 times)

0 Members and 1 Guest are viewing this topic.

obscurelyme

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Hello,

I'm looking for some advice on how to use sf::Text object. Specifically what is causing this crash to occur from Freetype library. Right now, any attempts to draw sf::Text results in a crash. I have included a link to my source code below as well as screenshots that showcase my setup after a build/crash. I am running 64-bit Windows with vcpkg as my main source of pulling in dependencies. I'm more than certain I am using debug libraries for a DEBUG build as vs code cmake tools sets by default.

My Source Code
https://github.com/obscurelyme/hello-sfml/blob/ed15963b38b896e414fab1b811ac5c8c7918e92d/main.cpp#L53-L73

Code Snippet:

// Load fonts
    std::string executableDirectory = std::filesystem::weakly_canonical(std::filesystem::path(argv[0])).parent_path().string();
    std::string relativeFontFile = "/assets/fonts/Roboto/Roboto-Regular.ttf";
    std::string fontFilePath = executableDirectory.append(relativeFontFile);
    sf::Font font;
    if (!font.loadFromFile(fontFilePath))
    {
        spdlog::debug("Could not load Roboto-Regular.ttf from directory {}", fontFilePath);
    }
    else
    {
        spdlog::debug("Successfully loaded {} font-family.", font.getInfo().family);
    }

    sf::Text text{"Hello", font, 48};
    text.setFillColor(sf::Color::Red);
    text.setStyle(sf::Text::Regular);
    text.setPosition(200, 200);
    ...
    while(running) {
       sf::Event event;
       while(window.pollEvent(event)) {
           // handle event[s] ...
       }

       window.clear(sf::Color::Blue);

        window.draw(text); // program crashes when this is called.

        window.display();
    }
 



Here is the screenshot of the crash in debugger
« Last Edit: September 07, 2021, 08:29:04 pm by obscurelyme »

obscurelyme

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #1 on: September 07, 2021, 09:03:24 pm »
Update:

I pulled down my source and compiled on OSX Big Sur v11.5.2 and it seems to work as intended. So right now I'm unsure if this is a platform issue or perhaps Mac is compiling with static libs vs on Windows I'm getting dynamic libs.

Here is the screenshot on OSX attached.

Would still like to get to the bottom on why this code doesn't work on x64 Windows 10.
« Last Edit: September 08, 2021, 02:52:42 am by obscurelyme »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #2 on: September 08, 2021, 09:56:44 am »
As vcpkg pulls in the freetype version provided by vcpkg, it might be that there's some incompatibility or otherwise different build. You could try to use the freetype library provided by SFML in the extlibs directory.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

obscurelyme

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #3 on: September 09, 2021, 11:32:12 pm »
Thanks for the advice. I'll give it a go configuring CMake to pull in freetype from SFML. More to come.

obscurelyme

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #4 on: September 10, 2021, 01:09:23 am »
Yikes, so vcpkg does have some in-development version control over what dependencies are to be installed. The lowest version of freetype that anyone can specify using vcpkg is 2.9.x and SFML uses 2.5.5 So Looks like vcpkg could be a non-starter for SFML type projects.
« Last Edit: September 10, 2021, 01:17:44 am by obscurelyme »

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #5 on: September 10, 2021, 07:56:27 am »
Have you tested a FreeType version on Windows other than 2.11?

There is a freetype bug that causes a crash in af_face_globals_get_metrics (where your callstack also points to) that only occurs on Windows (with libraries build with Visual Studio) and only with FreeType 2.11. https://gitlab.freedesktop.org/freetype/freetype/-/issues/1092

Downgrading to any version lower than that fixes that issue. FreeType versions are backwards compatible, I would expect SFML to work fine with FreeType 2.9 or 2.10
TGUI: C++ SFML GUI

obscurelyme

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #6 on: September 11, 2021, 04:55:19 pm »
I just updated my vcpkg.json to use Freetype 2.10 and it works! Many thanks.

theo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #7 on: September 16, 2021, 10:43:15 am »
May I ask how you did this? For me it won't work.
I edited the vcpkg.json in the ports\sfml dir to
(click to show/hide)

I got the value of builtin-baseline by doing
git rev-parse HEAD

This didn't do anything for me: it still grabbed freetype version 2.11.0.

Then I edited vcpkg\versions\baseline.json and changed the baseline of freetype to 2.10.0 and I edited the vcpkg.json of freetype to also take 2.10.0:
"version-semver": "2.10.0",

Now it SAYS that it takes 2.10.0. And vcpkg list says 2.10.0 for freetype. But the dll version is STILL 2.11.0 and drawing an sf::Text STILL crashes in freetype.

Maybe the easier way is to compile sfml with cmake and copy the freetype dll into the vcpkg\bin dir

theo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #8 on: September 16, 2021, 11:07:14 am »
stupid mistake: in vcpkg.json:

"overrides":[{"name":"sfml", "version":"2.10.0"}] -> "overrides":[{"name":"freetype", "version":"2.10.0"}]

But it doesn't make a difference, it still builds freetype 2.11.0

theo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #9 on: September 16, 2021, 01:38:22 pm »
Got it working, though my method is a bit hacky.

  • Downloaded the sources of freetype 2.10.4
  • opened the provided visual studio solution (builds\windows\vc2010 (yes this one works on vs2019 too)
  • built it for the desired configuration
  • now VS builds a dll named freetype.dll. I renamed it to freetyped.dll (debug version). Hovering in windows confirms that it is version 2.10.4.
  • copied it to vcpkg\installed\x64-windows\bin (overwrite existing file)
  • now it works. It seems that 2.10.4 and 2.11.0 are binary compatible.

Stackoverflow says that you have to edit header files. This is not necessary anymore. Just choose dll in the solution properties.

Hopefully they fix that bug soon. I do wonder however how they could miss this with any kind of testing on windows (not an entirely insignificant platform).

obscurelyme

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Drawing sf::Text results in a crash"writing_system_class" was nullptr
« Reply #10 on: September 19, 2021, 08:15:32 pm »
I have a branch that leverages freetype 2.10. You can take a look here. There isn't any modification needed just leverage the extensions to handle the heavy lifting.

It's important to remember that you need to have VCPKG_FEATURE_FLAGS set to "versions" as an environment variable in order for vcpkg versions to work.

https://github.com/obscurelyme/Semi-Pro-Cpp/tree/feature/sfml-freetype

Let me know if you have any questions.
« Last Edit: September 19, 2021, 08:23:45 pm by obscurelyme »