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

Author Topic: [SOLVED] Unhandled exception at 0x00007FFBCF11682B (sfml-graphics-d-2.dll) SFML  (Read 5897 times)

0 Members and 1 Guest are viewing this topic.

fuhrermag

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hello. I am working on a 2D game and now I'm dealing with the menu. I want it to have 3 text boxes.

Yes, I can do this:

        Font font;
        Text text1;
        font.loadFromFile("fonts/ARCADECLASSIC.ttf");
        text1.setFont(font);
        text1.setString("PLAY");
        text1.setCharacterSize(50);

        Text text2;
        text2.setFont(font);
        text2.setString("OPTIONS");
        text1.setCharacterSize(60);

        Text text3;
        text3.setFont(font);
        text3.setString("EXIT");
        text1.setCharacterSize(70);
 

It works, but it occupies too much space. So I thought I can make a function and instead of 12 lines I can use just 3:

Text Game::createText(const String &string, unsigned int size) {
        Font font;
        Text text;
        font.loadFromFile("fonts/ARCADECLASSIC.ttf");
        text.setFont(font);
        text.setString(string);
        text1.setCharacterSize(size);
        return text;
}

// some lines more

Text text1 = createText("PLAY", 50);
 

And now, I want to display text1:

window.draw(text1);
 

And here's the problem! I get this error:

"Unhandled exception at 0x00007FFBCF11682B (sfml-graphics-d-2.dll) in SFML Program.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.", and it shows an "X" on the line with "window.draw(text1);".

And no, it's not because I didn't put an if statement at "font.loadFromFile...". The font loads. It's another problem that I don't get it. Any help?
« Last Edit: August 03, 2018, 07:01:42 pm by fuhrermag »

verdog

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Try creating the font outside of your function and passing it in as a parameter.

SFML has a theme of internally storing pointers to graphical objects instead of copying them. I suspect that perhaps when you call setFont, the text object stores a pointer to the font object. This font object is then destroyed when the function ends (because you declared it in the function), causing a crash when SFML tries to draw it.

This is all purely speculative, but I think it would be worth a shot.

EDIT:
Yep; look at line 439 here

and the setFont function at line 129 here

The text object stores a pointer to its font. This pointer becomes invalid when the font is destroyed in your case.
« Last Edit: August 03, 2018, 06:52:09 pm by verdog »

fuhrermag

  • Newbie
  • *
  • Posts: 3
    • View Profile
Nah mate, Sadly this didn't work. I get the same error

Text Game::createText(Font f, const String &string) {
        Text text;
        text.setFont(f);
        text.setString(string);
        return text;
}

// lines more

        Font font;
        font.loadFromFile("fonts/ARCADECLASSIC.ttf");

        Text text1 = createText(font, "PLAY");
 

verdog

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Hmm. Try this:

Text Game::createText(Font &f, const String &string) {
        Text text;
        text.setFont(f);
        text.setString(string);
        return text;
}

// lines more

        Font font;
        font.loadFromFile("fonts/ARCADECLASSIC.ttf");

        Text text1 = createText(font, "PLAY");
 

(Pass the font by reference instead of by value)
Not sure if this will fix it, but again, I think it's worth a shot :)

fuhrermag

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hmm. Try this:

Text Game::createText(Font &f, const String &string) {
        Text text;
        text.setFont(f);
        text.setString(string);
        return text;
}


// lines more

        Font font;
        font.loadFromFile("fonts/ARCADECLASSIC.ttf");

        Text text1 = createText(font, "PLAY");
 

(Pass the font by reference instead of by value)
Not sure if this will fix it, but again, I think it's worth a shot :)


This worked. Thank you very much !

verdog

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
No problem  :D