Drawing sf::Text to window crashes program once window closes

0

I have recently been having a problem utilizing Texts and Fonts in a class. Attempting to assign a font to a text through .setFont() after reading in a font in the constructor works fine, and printing it out onto the window works as well, but after I close the window, an exception was thrown, and Visual Studio opened up an additional tab "xmemory" with the exception occurring on line 1311. Apparently something in my code caused the function "inline void _Container_base12::_Orphan_all()" to trigger. I'm aware that in order to use both a font and a text, the lifetime of the font must be preserved as long as the text, but since they are both members of the class Foo, they should share the same lifetime, no?

This is a recurring error in a project that I have been trying to complete, Sorting Algorithms Visualized. Using texts, I want to prompt the user with a menu of different sorting algorithms to view, but with the above issue, I cannot proceed with this idea.

//This is the class I am using to simplify my problem.
class Foo {
    public:
        Foo() : {
            if (!font.loadFromFile("C:\\Users\\Darien Miller\\Desktop\\fonts\\times-new-roman.ttf"))
                std::cout << "font file could not be found!";
                  t.setStyle(sf::Text::Bold);
                  //Here is the line that cause the crash once the window is closed.
                  t.setFont(font);
                  t.setString("Darien");
        }

        void printText(sf::RenderWindow &window) {
            window.draw(t);
        }

    private:
        Font font;
        Text t; 
};
//Here's the driver code
int main() {
    RenderWindow window(VideoMode(600, 600), "problem.exe", Style::Close | Style::Titlebar);

    Foo foo;

    while (window.isOpen()) {
        sf::Event e;
        while (window.pollEvent(e)) {
            if (e.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        foo.printText(window);
        window.display();
    }

}

After running the code, I expect to see the text printed to the window, and for the window to close without any issue. But each time, the tab "xmemory" opens up and I get the error message "Exception thrown at 0x00479A28 in SAV redux.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD."


EDIT: So, through trial and error, i figured out that simply drawing a text object the window through window.draw(someText)results in the access violation. More specifically, after I read in a font into a text such as in this trivial example:



int main() {

    RenderWindow window(VideoMode(600, 600), "halp pls", Style::Close | Style::Titlebar);
    Font font;

    font.loadFromFile("C:\\Users\\Darien Miller\\Desktop\\fonts\\times-new-roman.ttf");
    Text t("Darien", font);

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

        window.clear();
        window.draw(t);//This line causes the crash!
        window.display();
    }
}
c++
sfml
asked on Stack Overflow Aug 18, 2019 by Darien Miller • edited Aug 19, 2019 by Darien Miller

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0