SFML community forums

Help => Graphics => Topic started by: jjfortherear on January 05, 2013, 10:59:16 am

Title: SF::Text memory access bug on draw()
Post by: jjfortherear on January 05, 2013, 10:59:16 am
I've got a strange problem with drawing text, I made a video describing it and walking through the debugger:

http://www.youtube.com/watch?v=Ux8tNU3hyoc

The code can be found here:

https://github.com/odkken/Doors

Call stack:

    ntdll.dll!775915de()   Unknown
    [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]   
    ntdll.dll!775915de()   Unknown
    ntdll.dll!7758014e()   Unknown
>   sfml-graphics-d-2.dll!sf::priv::glCheckError(const char * file, unsigned int line) Line 107   C++
    sfml-graphics-d-2.dll!sf::RenderTarget::draw(const sf::Drawable & drawable, const sf::RenderStates & states) Line 146   C++
    RPG.exe!ShowingDoors::Draw(Game * game) Line 96   C++
    RPG.exe!Game::Draw() Line 30   C++
    RPG.exe!Game::Loop() Line 41   C++
    RPG.exe!main() Line 13   C++
    RPG.exe!__tmainCRTStartup() Line 536   C
    RPG.exe!mainCRTStartup() Line 377   C
    kernel32.dll!769833aa()   Unknown
    ntdll.dll!775a9ef2()   Unknown
    ntdll.dll!775a9ec5()   Unknown


EDIT:

I got it working by making my vector of doors a vector of pointers to doors.  Don't know why that was necessary, but there you go.

Title: Re: SF::Text memory access bug on draw()
Post by: eXpl0it3r on January 05, 2013, 11:16:30 am
First step in debugging things you don't understand is to create a minimal example from scratch, just to see if there's a problem directly with SFML, or if it's just within your code, e.g.:
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 768), "Hello World");
    sf::Font font;
    font.loadFromFile("fonts/arial.ttf");
    sf::Text text;
    text.setFont(font);

    while(window.isOpen())
    {
         sf::Event event;
         while(window.pollEvent(event))
         {
             if(event.type == sf::Event::Closed)
                  window.close();
         }

         window.clear();
         window.draw(text);
         window.display();
    }
}

If that does work without problems, then it's something within your structure.

Also if you actually try to draw a text on top of the door rectangle, then you can't draw the text first and then the rectangle, otherwise the rectangle will cover up the text.

Edit: Not sure if this could be part of the problem, but since you have the sf::Font as member of the Doors class, you load the font for every door instance, thus you end up with x copies of the same font. It's not really good to do this, but I'm not sure if it could cause such a problem.
Title: Re: SF::Text memory access bug on draw()
Post by: jjfortherear on January 05, 2013, 11:22:14 am
Also if you actually try to draw a text on top of the door rectangle, then you can't draw the text first and then the rectangle, otherwise the rectangle will cover up the text.

I realize that: the doors are supposed to cover the text.

I'll post a minimal example tomorrow, gotta hit the sack for now.
Title: Re: SF::Text memory access bug on draw()
Post by: eXpl0it3r on January 05, 2013, 11:27:50 am
I'll post a minimal example tomorrow, gotta hit the sack for now.
Okay, also it could be useful if you would provide the project file, so we could directly run your code. ;)

Btw whenever you start with a SFML project, you nearly always want to start with an empty project instead of a Win32 application.
Title: Re: SF::Text memory access bug on draw()
Post by: Laurent on January 05, 2013, 11:31:56 am
Compiler? OS? Version of SFML?
Title: Re: SF::Text memory access bug on draw()
Post by: eXpl0it3r on January 05, 2013, 11:35:23 am
Compiler? OS? Version of SFML?
YouTube video? ;D
He uses VS 2012 and well then obviously Windows.

But the version is a good question, did you recompile SFML on your own? Because the RC version doesn't properly work with VS 2012.
Title: Re: SF::Text memory access bug on draw()
Post by: jjfortherear on January 05, 2013, 11:38:39 am
I built SFML with cmake, I think it's a couple weeks old (not sure where to find the exact build#).

I fixed (circumnavigated) the issue by making it a vector of pointers to the objects, rather than a vector of objects.  I still don't know why this would cause an issue - as you saw, the sprites drew just fine.
Title: Re: SF::Text memory access bug on draw()
Post by: eXpl0it3r on January 05, 2013, 11:44:03 am
I fixed (circumnavigated) the issue by making it a vector of pointers to the objects, rather than a vector of objects. I still don't know why this would cause an issue - as you saw, the sprites drew just fine.
Well then its because you didn't implement a copy constructor and when you push_back things into a vector the elements get copied around and if the space within the vector is to small, everything will get moved. I further guess that the problem lies with the local defined sf::Font. Fonts, textures, sounds etc are (heavy) resource objects and one should always manage them separatly and only pass reference (or smart pointers) around.