Android crash on setFont()

Started by Radegast, 06 February 2016, 20:58:15

Radegast

I think I found a bug with TGUI on Android. Let's use the minimal example from the documentation and just add this:

Code (cpp) Select
    sf::Font font;

    if(!font.loadFromFile("assets/sansation.ttf"))
    {
        std::cerr << "Could not load font" << std::endl;
        return 1;
    }

    gui.setFont(font);


It works fine on OS X, but on Android it will crash with this log: https://pastebin.com/WBe1BGnT

If you don't use custom font then it works without any issues.


texus

Does the following work?
Code (cpp) Select
auto font2 = std::make_shared<sf::Font>(font);

Radegast

If you mean like this:

Code (cpp) Select
    auto font2 = std::make_shared<sf::Font>(font);
    gui.setFont(font2);


Then it crashes as well:

Quote02-06 21:09:13.381    66    66 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
02-06 21:09:13.381    66    66 F DEBUG   : Build fingerprint: 'generic/sdk_phone_armv7/generic:6.0/MRA44C/2166767:eng/test-keys'
02-06 21:09:13.382    66    66 F DEBUG   : Revision: '0'
02-06 21:09:13.382    66    66 F DEBUG   : ABI: 'arm'
02-06 21:09:13.383    66    66 F DEBUG   : pid: 1326, tid: 1340, name: .simek.theycome  >>> xyz.simek.theycome <<<
02-06 21:09:13.383    66    66 F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
02-06 21:09:13.881    66    66 F DEBUG   :     r0 00000000  r1 00000000  r2 00000000  r3 00000000
02-06 21:09:13.882    66    66 F DEBUG   :     r4 a4c3f578  r5 00000000  r6 a4c3f888  r7 ae91d740
02-06 21:09:13.882    66    66 F DEBUG   :     r8 a5442de4  r9 a4c3f570  sl a4c3f888  fp ad097de4
02-06 21:09:13.883    66    66 F DEBUG   :     ip ad111ff0  sp a4c3f4e8  lr ad10e721  pc ac768d8c  cpsr 60000030
02-06 21:09:13.960    66    66 F DEBUG   :
02-06 21:09:13.960    66    66 F DEBUG   : backtrace:
02-06 21:09:13.961    66    66 F DEBUG   :     #00 pc 00008d8c  /system/lib/libandroid.so (AAsset_seek+1)
02-06 21:09:13.961    66    66 F DEBUG   :     #01 pc 0000871d  /data/app/xyz.simek.theycome-2/lib/arm/libsfml-system.so (sf::priv::ResourceStream::seek(long long)+8)
02-06 21:09:13.962    66    66 F DEBUG   :     #02 pc 00015d63  /data/app/xyz.simek.theycome-2/lib/arm/libsfml-graphics.so (sf::Font::loadFromStream(sf::InputStream&)+62)
02-06 21:09:13.962    66    66 F DEBUG   :     #03 pc 000065e3  /data/app/xyz.simek.theycome-2/lib/arm/libsfml-example.so (main+186)
02-06 21:09:13.963    66    66 F DEBUG   :     #04 pc 0000680b  /data/app/xyz.simek.theycome-2/lib/arm/libsfml-example.so (sf::priv::main(sf::priv::ActivityStates*)+70)
02-06 21:09:13.963    66    66 F DEBUG   :     #05 pc 00007e6f  /data/app/xyz.simek.theycome-2/lib/arm/libsfml-system.so
02-06 21:09:13.964    66    66 F DEBUG   :     #06 pc 0003e7f7  /system/lib/libc.so (__pthread_start(void*)+30)
02-06 21:09:13.964    66    66 F DEBUG   :     #07 pc 00018d9d  /system/lib/libc.so (__start_thread+6)

texus

#3
I meant without the setFont call, because setFont really shouldn't do much more than copying the font like that.
So if the copy itself is failing (which the backtrace seems to indicate because it contains sf::Font::loadFromStream) then this is still an issue in sfml and I won't be able to help with it.

Edit: based on the backtrace I expected loadFromStream to be used when copying the font but it doesn't seem to do that when looking at the source code. But it has to be called from somewhere.

Edit2: Are you certain the problem isn't already happening inside font.loadFromFile, it does call loadFromStream internally?

Radegast

You are right the crash was earlier than setFont(), it's in font.loadFromFile(), so it is not TGUI fault after all. Just for reference this is all I had:

Code (cpp) Select
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Network.hpp>
#include <TGUI/TGUI.hpp>

int main()
{
    sf::RenderWindow window{{800, 600}, "Window"};
    tgui::Gui gui{window}; // Create the gui and attach it to the window
   
    sf::Font font;

    if(!font.loadFromFile("assets/sansation.ttf"))
    {
        std::cerr << "Could not load font" << std::endl;
        return 1;
    }

    gui.setFont(font);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            gui.handleEvent(event); // Pass the event to the widgets
        }

        window.clear();
        gui.draw(); // Draw all widgets
        window.display();
    }
}


Radegast

Never mind that. It actually crashes BOTH in loadFromFile() and setFont(). I created a bug report at SFML bug tracker and it turns out loadFromFile() crashed because I provided a wrong path to the font file (assets folder is the top directory on Android by default).  After correcting that I was able to use sf::Text without an issue. However, when I switched back to TGUI, it crashed again and this time I am 100% it is in setFont() - use the minimal example above with if(!font.loadFromFile("sansation.ttf")). The backtrace is different this time:

Quote
:     #00 pc 000318d6  /data/app/xyz.simek.theycome-1/lib/arm/libsfml-graphics.so (FT_Stroker_Done+9)
:     #01 pc 0001590d  /data/app/xyz.simek.theycome-1/lib/arm/libsfml-graphics.so (sf::Font::cleanup()+72)
:     #02 pc 00015931  /data/app/xyz.simek.theycome-1/lib/arm/libsfml-graphics.so (sf::Font::~Font()+4)
:     #03 pc 0006422b  /data/app/xyz.simek.theycome-1/lib/arm/libc++_shared.so (std::__1::__shared_weak_count::__release_shared()+46)
:     #04 pc 0005dd1d  /data/app/xyz.simek.theycome-1/lib/arm/libtgui.so (tgui::Widget::setFont(tgui::Font const&)+36)
:     #05 pc 000423df  /data/app/xyz.simek.theycome-1/lib/arm/libtgui.so (tgui::Container::setFont(tgui::Font const&)+6)
:     #06 pc 000067cf  /data/app/xyz.simek.theycome-1/lib/arm/libsfml-example.so (main+322)
:     #07 pc 00006903  /data/app/xyz.simek.theycome-1/lib/arm/libsfml-example.so (sf::priv::main(sf::priv::ActivityStates*)+70)
:     #08 pc 00007e6f  /data/app/xyz.simek.theycome-1/lib/arm/libsfml-system.so
:     #09 pc 0003e7f7  /system/lib/libc.so (__pthread_start(void*)+30)
:     #10 pc 00018d9d  /system/lib/libc.so (__start_thread+6)


Radegast

Quote from: texus on 06 February 2016, 21:06:24
Does the following work?
Code (cpp) Select
auto font2 = std::make_shared<sf::Font>(font);

Oh yea and if I replace the gui.setFont(font) line with this there is no crash.

texus

#7
According to the stacktrace it is happening when the default font is destroyed. It still looks like a problem with sfml, but I will first have to come up with an example without tgui that can reproduce this crash to prove that.

The following code still uses something from tgui, but if this also fails then I'm on the right track:
Code (cpp) Select
#include <TGUI/DefaultFont.hpp>
#include <SFML/Graphics.hpp>
#include <memory>
#include <iostream>

int main()
{
    std::shared_ptr<sf::Font> font;

    sf::Font font1;
    if (!font1.loadFromMemory(defaultFontBytes, sizeof(defaultFontBytes)))
    {
        std::cerr << "Could not load default font" << std::endl;
        return 1;
    }
    font = std::make_shared<sf::Font>(font1);

    {
        sf::Font font2;
        if(!font2.loadFromFile("sansation.ttf"))
        {
            std::cerr << "Could not load font" << std::endl;
            return 1;
        }

        font = std::make_shared<sf::Font>(font2);
    }

    return 0;
}


PS: If the forum is loading too slow (it seems to have become really slow in the last 2 days), you can contact me directly on vdv_b@tgui.eu.

texus

I tried to debug it myself today but I can't reproduce the issue. I tried the code that you posted without the "assets/" and it runs fine on my emulator.

I would find it strange if this would be specific to hardware, so perhaps the difference could still be in the library versions that we use. Which sfml and tgui versions are you using?

Btw, did you have to do anything that isn't mentioned in the tutorial to compile tgui for android? Because I only tested it on linux so far and I wonder if there are differences with compiling it on mac.

texus

#9
Intresting, I have been able to reproduce it with the latest SFML version, but not with the old version that was still on my pc (which according to Config.hpp is 2.3.1).

The following code reproduces it, so it is definately an SFML problem.
Code (cpp) Select
#include <SFML/Graphics.hpp>
#include <memory>

int main()
{
    std::shared_ptr<sf::Font> font3;

    sf::Font font1;
    font1.loadFromFile("sansation.ttf");

    font3 = std::make_shared<sf::Font>(font1);

    sf::Font font2;
    font2.loadFromFile("sansation.ttf");

    font3 = std::make_shared<sf::Font>(font2);
}


I'm going to see if I can find out in which commit the bug was introduced.

Edit: The bug was introduced in 957cabb, earlier SFML versions don't crash.

Edit2: Reported on SFML forum

Radegast

Yeah, I was using both TGUI and SFML from git.

Great job, thanks for figuring that out!