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

Author Topic: Usage of sf::Text and sf::Font in class crashes program once window closes  (Read 15009 times)

0 Members and 2 Guests are viewing this topic.

darienmiller88

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
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);
                        t.setString("HELP!!");
                        t.setFont(font);
                }

                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."

Even doing a trivial program utilizing texts and fonts, I get the same issue. Assigning a font to the text works fine, but once I print the text onto the window, the program crashes when the window is closed. I've tried everything, but I can't figure out why this is happening

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Is the provided code complete?

0xCDCDCDCD would mean it's accessing uninitialized memory.
Why that would happen, I don't really know.

One thing with font and text is, that the font object needs to exist as long as there's a text object using it, but given the construction & destruction order of your class, that should be correct - unless this is just pseudo code and your actual code looks very different.

What's the call stack when it fails?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

darienmiller88

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hey eXpl0it3r, thanks for replying! The code I provided is indeed a full, complete program. I was attempting to use texts in another larger program, and I got the same error, so I created a very basic program to utilize a text to see if I would get the same error. Unfortunately, I did receive that same error message even with this stripped down program, once I print the text to the screen. Assigning the text to the font works, but once I print it to the screen, the 0xCDCDCDCD error is generated. Here's the call stack after running it again:

    Tic tac toe.exe!std::_Container_base12::_Orphan_all() Line 1311   C++
    Tic tac toe.exe!std::vector<sf::Vertex,std::allocator<sf::Vertex> >::_Tidy() Line 1660   C++
    Tic tac toe.exe!std::vector<sf::Vertex,std::allocator<sf::Vertex> >::~vector<sf::Vertex,std::allocator<sf::Vertex> >() Line 628   C++
    Tic tac toe.exe!sf::VertexArray::~VertexArray()   C++
    Tic tac toe.exe!sf::Text::~Text()   C++
    Tic tac toe.exe!Foo::~Foo()   C++
    Tic tac toe.exe!main() Line 51   C++
    [External Code]   
>   kernel32.dll![Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]   Unknown

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
What version of SFML are you using and from where did you get it?

« Last Edit: August 29, 2019, 12:51:47 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

darienmiller88

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
I have SFML version 2.5.1 and I got it from the official website: https://www.sfml-dev.org/download/sfml/2.5.1/
I downloaded the following version: Visual C++ 15 (2017) - 32-bit.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Usage of sf::Text and sf::Font in class crashes program once window closes
« Reply #5 on: September 04, 2019, 03:25:53 pm »
I've not seen this issue and can't really think of anything beyond a potential mismatch of versions somewhere.
My suggestion would be to rebuild SFML for your current compiler.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/