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