SFML community forums

Help => General => Topic started by: beamer159 on May 13, 2015, 11:21:04 pm

Title: Unhandled Exception when trying to draw a Text object from within a Drawable
Post by: beamer159 on May 13, 2015, 11:21:04 pm
I am receiving an unhandled exception error when trying to draw a Text object. Here is the error message:

Quote
Unhandled exception at 0x59085815 (sfml-graphics-2.dll) in Program.exe: 0xC0000005: Access violation reading location 0x3F800058.

The thing is, I am telling main to draw a Drawable object (InputBox), which is telling another Drawable object (Button) to draw the Text. If I replace the Text object with another Drawable, such as a CircleShape, the program works. Also, if I draw the Button in main directly, it works as well. Here is my code:

My main Program
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
        InputBox inputBox;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::Black);
        window.draw(inputBox);
        window.display();
    }

    return 0;
}

InputBox class
class InputBox : public sf::Transformable, public sf::Drawable
{
public:
        InputBox()
        {
                button = Button();
        }
        virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const
        {
                states.transform *= getTransform();
                target.draw(button, states);
        }
private:
        Button button;
};

Button class
class Button : public sf::Transformable, public sf::Drawable
{
public:
        Button()
        {
                font.loadFromFile("arial.ttf");
                text.setFont(font);
                text.setString("Button");
        }
        virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const
        {
                states.transform *= getTransform();
                target.draw(text, states);
        }
private:
        sf::Font font;
        sf::Text text;
};
Title: Re: Unhandled Exception when trying to draw a Text object from within a Drawable
Post by: eXpl0it3r on May 14, 2015, 12:38:47 am
Are you using x64 libs with an x86 application?
Where the DLLs created with the same compiler you're using?
Are you mixing debug and release modes?
Are you mixing SFML version or using old header files?
Title: Re: Unhandled Exception when trying to draw a Text object from within a Drawable
Post by: beamer159 on May 14, 2015, 12:50:48 am
I am using x86 libs.
I am using VS 2012, and I downloaded the SFML 2.3 package for VS 2012.
I am only using debug mode for the time being.
I don't think I am using old headers. Only those that came with the SFML 2.3 package for VS 2012.

Edit: I just tested this code on another computer with a different version of Visual Studio, and I got the same error. This makes me think the problem is with my code somewhere.
Title: Re: Unhandled Exception when trying to draw a Text object from within a Drawable
Post by: ratzlaff on May 14, 2015, 04:22:39 pm
    InputBox()
    {
//        button = Button();
    }
 

Try commenting out that line
Title: Re: Unhandled Exception when trying to draw a Text object from within a Drawable
Post by: beamer159 on May 14, 2015, 04:53:50 pm
That fixed my issue. Thank you.