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

Author Topic: SF::Text memory access bug on draw()  (Read 5636 times)

0 Members and 4 Guests are viewing this topic.

jjfortherear

  • Newbie
  • *
  • Posts: 5
    • View Profile
SF::Text memory access bug on draw()
« 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:



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.

« Last Edit: January 05, 2013, 11:39:16 am by jjfortherear »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SF::Text memory access bug on draw()
« Reply #1 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.
« Last Edit: January 05, 2013, 11:20:55 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jjfortherear

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SF::Text memory access bug on draw()
« Reply #2 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SF::Text memory access bug on draw()
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SF::Text memory access bug on draw()
« Reply #4 on: January 05, 2013, 11:31:56 am »
Compiler? OS? Version of SFML?
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SF::Text memory access bug on draw()
« Reply #5 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jjfortherear

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: SF::Text memory access bug on draw()
« Reply #6 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: SF::Text memory access bug on draw()
« Reply #7 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything