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 to 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();
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Darien Miller
  • 651
  • 2
  • 7
  • 16
  • https://stackoverflow.com/questions/370195/when-and-why-will-an-os-initialise-memory-to-0xcd-0xdd-etc-on-malloc-free-new – πάντα ῥεῖ Aug 18 '19 at 23:05
  • You are basically drawing to the window when `!isOpen()`. Probably var `window` freed some buffers at `window.clear()` that are needed to perform the `window.draw()`. Try to identify which line throws the access violation exception and tell us. – andresantacruz Aug 18 '19 at 23:10
  • @dedecos commenting out the line `foo.printText(window)` seems to do the trick, but I can't seem to figure out why simply drawing the text to the window is causing the access violation. – Darien Miller Aug 19 '19 at 00:53
  • @DarienMiller try to `foo.printText(window)` before `window.clear()` and see if it works. – andresantacruz Aug 19 '19 at 00:55
  • @πάνταῥεῖ So it seems that there is "Clean memory" that's not being written by the application. But why is that the case though? – Darien Miller Aug 19 '19 at 00:55
  • @dedecos unfortunately, i'm getting the same error. Having a `.draw()` call before `window.clear()` just results in nothing being printed to the screen. This is an interesting bug... – Darien Miller Aug 19 '19 at 01:13
  • I meant: put the call to `printText` inside the while loop. – andresantacruz Aug 19 '19 at 01:21
  • @dedecos It's already in the while loop though? Unless you meant the inner while loop, cause that still resulted in the same access violation. It seems that whenever i attempt to read in a font to the text object in the class, the crash happens. When I remove `t.setFont(font)`, it runs fine, but obviously I need the font so the text can actually be printed to the screen. – Darien Miller Aug 19 '19 at 02:24
  • Oh sorry bud I read it too fast and didn't notice that haha. It seems that you can be missing something regarding the correct usage of this specific api/framework you are using, try to run any basic sample of this api and see whats going on. It's complicated to help you here because you didn't even told us which are the api/framework you are using. – andresantacruz Aug 19 '19 at 02:45
  • @DarienMiller did you try other fonts? – Nikita Smirnov Aug 19 '19 at 19:02
  • @GoverNator, yes i did, and sadly the issue still persisted. I'm not sure what other options are there :/ – Darien Miller Aug 19 '19 at 20:37

0 Answers0