Adding widget to group crashes

Started by r4gTime, 16 May 2020, 15:35:46

r4gTime

Hello,

I have a very simple problem but I couldn't find the solution by myself or on this forum...

Adding any widget to my group makes my application crash.

I have a classe named Application, it has a tgui object called tgui.
When I create Application, I try to do this :
Code (cpp) Select
Application::Application()
{
    // Some code

    // Graphical User Interface
    Application::tgui.setTarget(Application::window); // setting my tgui object on my window object
    try
    {
        auto button1 = tgui::Button::create();
        auto group = tgui::Group::create();

        group->add(button1, "button"); // CRASH

    }
    catch (const tgui::Exception& e)
    {
        std::cerr << "TGUI Exception: " << e.what() << std::endl;
    }
}


I don't have any answer from cerr, and I don't understand why this would crash ?

Thank you for your help !

texus

You should check the call stack from the debugger to see if it gives any more information.
Are window and tgui static member variables? They should be initialized by the class constructor and not at program startup before the main function gets executed (which happens with globals or static members), as that could give issues.

r4gTime

Thanks for your answer !

My tgui object is member of my class Application :
Code (cpp) Select
class Application
{
    public:
        Application();

        sf::RenderWindow window;

        tgui::Gui tgui; // GUI

    protected:

    private:

};


So it's created in my main when I create an instance of Application :
Code (cpp) Select

int main()
{
    Application app;

    Mainmenu(&app);

    return 0;
}


Is that bad ? If I initialize it in the constructor then it will be deleted after I call it ?

texus

Your method is correct, the window and gui will be constructed when the "app" object is created and destructed at the end of the main function.
The reason why I though you might be using static variables is because it is weird that you typed "Application::" in front of "tgui" and "window" in your original code. People don't usually add the class name before their member names unless it is a static variable or when calling a function from a base class that is overridden in the derived class. You would normally just type "tgui", or "this->tgui" if people really want to show that it is a member variable. So the syntax was a bit unusual, but there is nothing wrong with the code you showed.

You should have a look at the call stack then as I wouldn't know what else could cause it.

r4gTime

My debugger tells me it's a SIGSEGV (Segmentation Fault), when the app come to :  group->add(button1, "button");

I have absolutely no idea of what this means :/

texus

What IDE are you using? Visual Studio?

r4gTime


texus

In the menu bar, check Debug > Debugging Windows > Call Stack.
When it crashes, it shows which functions gets called where. If you are in debug mode and linking to the TGUI libraries ending with "-d" then it will even show where inside the TGUI library it crashes.
If you can send the entire call stack then I may be able to see what went wrong.

r4gTime

I think I linked everything but I only have this in call stack :

Code (none) Select
#0 ?? ?? () (??:??)
#1 0x410c60 Application::Application(this=0x14268d0) (D:\Documents\Programmation\X22 Colonization\X22_Data.cpp:48)
#2 0x401437 main() (D:\Documents\Programmation\X22 Colonization\main.cpp:36)


and logs :
Code (none) Select
Debugger name and version: GNU gdb (GDB) 7.9.1
Child process PID: 17848
Program received signal SIGSEGV, Segmentation fault.
In ?? () ()
#1  0x00410c60 in Application::Application (this=0x14268d0) at D:\Documents\Programmation\X22 Colonization\X22_Data.cpp:48
D:\Documents\Programmation\X22 Colonization\X22_Data.cpp:48:1667:beg:0x410c60
At D:\Documents\Programmation\X22 Colonization\X22_Data.cpp:48
#1  0x00410c60 in Application::Application (this=0x14268d0) at D:\Documents\Programmation\X22 Colonization\X22_Data.cpp:48
D:\Documents\Programmation\X22 Colonization\X22_Data.cpp:48:1667:beg:0x410c60
Debugger finished with status 0


I must have done a mistake somewhere when installing or something ? But  everything's works fine except this damn line, so I don't understand. Thanks for your help anyway, I keep on looking for the problem...

texus

Are you in debug or release mode? Which libraries are you linking exactly?

r4gTime

Yes I'm in debug mode, and here is a picture of my linker settings.


texus

Could you send the full source code that you are using so that I can try to run it here and see if it crashes here as well?

r4gTime

It would be difficult for me to send all sources, but I tried to duplicate the problem based on your login screen example (https://tgui.eu/examples/0.8/scalable-login-screen/) and I have the same problem, just by modifying this function :

Code (cpp) Select

void loadWidgets( tgui::Gui& gui )
{
    auto button1 = tgui::Button::create();
    auto group = tgui::Group::create();

    group->add(button1, "button");

    gui.add(group);
}


Segmentation fault :/

texus

Then the only thing I can think of is if the libraries aren't compatible.
Are you using the SFML and TGUI libraries that you downloaded from sfml and tgui websites? Are you using the GCC compiler that comes with codeblocks 17.12? Did you download the SFML and TGUI libraries for exactly this compiler (MinGW 5.1.0 TDM for the one that can be installed together with CodeBlocks)?
Could you send me the .cbp file from your project?

r4gTime

Hello,

Here it is. Hope you can find something and thank you again for the support :)