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

Author Topic: sf::Text::setString gives an access violation (very detailed description inside)  (Read 2193 times)

0 Members and 1 Guest are viewing this topic.

Bluesroo

  • Newbie
  • *
  • Posts: 4
    • View Profile
I'm using Visual C++ 2010 as my IDE. I'm sorry in advance for the wall of code you're about to see, I just figured I would be as detailed as possible. I'll just cut to the chase with the code, and explain at the bottom.

Game function, which loads the font, declares playText and quitText, and then sets their position and string before going onto the rest of the function.
int mainMenu(sf::RenderWindow *window, CircleWrap mouseCircle)
{
        int choice = 0;
        sf::Font font;
        font.loadFromFile("arial.ttf");
        TextWrap playText(font), quitText(font);
        playText.play();
        quitText.quit();
...

This is the TextWrap class, which inherits sf::Text:
class TextWrap : public sf::Text
{
public:
        TextWrap(sf::Font font);
        void play(void);
        void quit(void);
};

This is where the functions of TextWrap are defined:
TextWrap::TextWrap(sf::Font font)
{
        setFont(font);
        setCharacterSize(30);
        setStyle(sf::Text::Bold);
        setColor(sf::Color::White);
}

void TextWrap::play(void)
{
        setString("Play!");
        setPosition(300, 400);
        return;
}

void TextWrap::quit(void)
{
        setString("Quit");
        setPosition(300, 200);
        return;
}

This is the error that I get:
First-chance exception at 0x0120afe3 in mouseAvoider.exe: 0xC0000005: Access violation reading location 0xccccccd0.
Unhandled exception at 0x0120afe3 in mouseAvoider.exe: 0xC0000005: Access violation reading location 0xccccccd0.

The actual error is thrown at the setString line. It doesn't matter whether I do the quit or the play functions first, they both throw the exception. As far as I can tell, I cannot tell why I would get an access violation. When I open the callstack, this is the last call that was made:
>       mouseAvoider.exe!std::_Iterator_base12::_Adopt(const std::_Container_base12 * _Parent)  Line 146 + 0x6 bytes

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
The error I can see is that a sf::Text need to keep a pointer on a attributes of sf::Font, try using reference on sf::Font rather than using copy constructor.

I mean, use a reference for your TextWrap constructor.
 TextWrap(sf::Font& font);

Also, be sure to check if your font is correctly loaded, sometimes it can solve problems. :D



Bluesroo

  • Newbie
  • *
  • Posts: 4
    • View Profile
The error I can see is that a sf::Text need to keep a pointer on a attributes of sf::Font, try using reference on sf::Font rather than using copy constructor.

I mean, use a reference for your TextWrap constructor.
 TextWrap(sf::Font& font);

Also, be sure to check if your font is correctly loaded, sometimes it can solve problems. :D

You're awesome! Thank you!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Since you don't change the font, use a reference to const:
const sf::Font& font

By the way, I would probably rather keep sf::Text as a member than inherit it. It depends on whether you fulfill the LSP, and don't just use inheritance to write less code.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything