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

Author Topic: [SOLVED] Crash when draw() is used  (Read 2582 times)

0 Members and 1 Guest are viewing this topic.

Antonio9227

  • Newbie
  • *
  • Posts: 25
    • View Profile
[SOLVED] Crash when draw() is used
« on: January 28, 2015, 08:49:01 am »
Hello, I am a object oriented programming newbie and I've encountered a weird problem while trying to make a level class. My application crashes every time I try to draw a level object, here is the code I'm using:
virtual void draw(sf::RenderTarget &target,sf::RenderStates states) const
{
    states.transform *=getTransform();
    states.texture = m_texture;
    target.draw(m_vertices,states); //Crashes here
}
 

During debug I receive an SIGSEGV segmentation fault signal when that specific line is executed. If I disable it the program runs as expected. m_vertices is a sf::VertexArray and m_texture is a sf::Texture pointer.
Also it doesn't matter if I constructed a mesh inside the vertex array or not, it still crashes. I've used the exact same piece of code for another class and it works just fine.

I really don't know what the problem could be and some help would be appreciated. 
Thanks in advance, regards Antonio.
« Last Edit: February 02, 2015, 10:52:56 pm by Antonio9227 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
AW: Crash when draw() is used
« Reply #1 on: January 28, 2015, 09:26:49 am »
Run it through the debugger, check the call stack and make sure all your used objects are valid.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Crash when draw() is used
« Reply #2 on: January 29, 2015, 02:19:57 am »
I fancy a guess that it's the m_texture pointer that's invalid.

I'm allowed to guess, right?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Antonio9227

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Crash when draw() is used
« Reply #3 on: February 02, 2015, 10:51:31 pm »
I had some spare time today so I read my code. I found my mistake and it had nothing to do with SFML. I had a function to load the level and that function did not worked properly. At a point my function was accessing items from an array (that was holding my tile information), my index was out of bound and I think it somehow managed to modify some bytes of the place where my vertex array was stored in the memory. Is this even possible?
Anyways, this is the only explanation I could come up with, thanks anyways for trying to help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Crash when draw() is used
« Reply #4 on: February 03, 2015, 09:52:24 am »
my index was out of bound and I think it somehow managed to modify some bytes of the place where my vertex array was stored in the memory. Is this even possible?
To some degree, yes. Accessing the element beyond the last index of an array or vector is by the standard legal, but modifying that position will cause undefined behavior and it you're lucky you'll corrupt the stack and application will crash. If you're unlucky it will keep running and you never notice that you're doing something wrong.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything