Search code examples
c++linkersfml

Read Access violation when running gui.get<typename>("") with sfml backend in TGUI


I'm currently trying to use TGUI with SFML as the backend, everything works fine when I had this code

#include <iostream>
#include <TGUI/TGUI.hpp>
int main() {
    sf::RenderWindow window{ {800, 600}, "TGUI window with SFML" };
    tgui::GuiSFML gui{ window };
    gui.loadWidgetsFromFile("menus/startMenu.txt");
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            gui.handleEvent(event);
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
        

        window.clear();
        gui.draw();
        window.display();
    }
}

However, I then tried to add a line to refer to a button loaded from tgui::Button::Ptr aButton = gui.get<tgui::Button>("a");.

#include <iostream>
#include <TGUI/TGUI.hpp>
int main() {
    sf::RenderWindow window{ {800, 600}, "TGUI window with SFML" };
    tgui::GuiSFML gui{ window };
    gui.loadWidgetsFromFile("menus/startMenu.txt");
    tgui::Button::Ptr aButton = gui.get<tgui::Button>("a"); // right here
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            gui.handleEvent(event);
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }
        

        window.clear();
        gui.draw();
        window.display();
    }
}

and it gives me this error

Exception thrown at 0x7956271B (tgui.dll) in maze 2.exe: 0xC0000005: Access violation reading location 0x000003E4

I'm currently dynamically linking tgui and sfml, using TGUI-0.9 and SFML-2.5.1 with Debug x86 on Visual Studio c++.

The error also tells me it's coming from

        template <class T>
        typename T::Ptr get(const String& widgetName) const
        {
            return std::dynamic_pointer_cast<T>(get(widgetName));
        }

in Container.hpp in TGUI. I think that the problem is the dynamic_pointer_cast throwing an error, but I don't know how to fix it. I also don't understand why everything else works except the gui.get<typename>("sometext"); function. Any help?

Edit 1: I've gone ahead and tested with gui.get(), which works perfectly fine. This means that the problem is definitely in the dynamic_pointer_cast, since gui.get<typename>() just calls gui.get() and runs dynamic_pointer_cast on it.


Solution

  • Ok so I just had to restart Visual studio, and it now works perfectly