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

Author Topic: Inherited sf::Drawable crashing  (Read 8202 times)

0 Members and 1 Guest are viewing this topic.

Camble

  • Newbie
  • *
  • Posts: 13
    • View Profile
Inherited sf::Drawable crashing
« on: January 19, 2013, 05:57:36 pm »
Hi there,

I'm using SFML 2.0 and have created a SpriteSheet class which inherits sf::Drawable. It contains a pointer array to store sprites created dynamically and I'm having trouble getting the sprites to draw.

SpriteSheet Declaration:
class SpriteSheet : public sf::Drawable {
    public:
        SpriteSheet(sf::Texture* texture, const int tileSize);
        ~SpriteSheet();

        void draw(sf::RenderTarget& target, sf::RenderStates states) const;

    private:
        int theTileSize;
        int cols;
        int rows;

        sf::Texture *pTexture;
        sf::Sprite *pSprites[16][16];
};

This is how I'm populating the sprites pointer array within the SpriteSheet constructor.
    for (int i=0; i<rows; i++) {
        for (int j=0; j<cols; j++) {
            sf::IntRect rect(sf::Vector2i(x,y),sf::Vector2i(theTileSize, theTileSize));
            pSprites[i][j] = new sf::Sprite(*pTexture, rect);
            pSprites[i][j]->setPosition(x*tileSize,y*tileSize);
        }
    }


When I run my code with window.draw(SpriteSheet); I get segfault errors. This is my draw function. If I use the same syntax in main(), everything works fine.
void SpriteSheet::draw(sf::RenderTarget &target, sf::RenderStates states) const {
    target.draw(*pSprites[0][0]);
}

Any ideas? I've only recently picked C++ back up again and I'm probably missing something glaringly obvious.

Thanks.
« Last Edit: January 20, 2013, 03:31:39 am by Camble »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Inherited sf::Drawable with SFML 2.0
« Reply #1 on: January 19, 2013, 06:21:34 pm »
Don't use new, delete or arrays. Take STL containers like std::vector or std::array instead. They manage memory automatically.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Camble

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Inherited sf::Drawable with SFML 2.0
« Reply #2 on: January 19, 2013, 06:23:19 pm »
Well the array was really only for testing purposes, but I'll give it a go.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Inherited sf::Drawable with SFML 2.0
« Reply #3 on: January 19, 2013, 06:25:47 pm »
I think Nexus meant that as an overall comment. ;)

Are you sure [0 ][0] and texture are valid? Also posting complete crashing example(single file with main, your class, #include <SFML/Graphics.hpp>) would help finding the problem, it can be outside of snippets you posted and it's easier to run and debug it, let the computer find the problem for us.
Quote
If I use the same syntax in main(), everything works fine.
What do you mean by same syntax?
Back to C++ gamedev with SFML in May 2023

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Inherited sf::Drawable with SFML 2.0
« Reply #4 on: January 19, 2013, 06:31:31 pm »
I wrote a class class for Sprite Animation recently. It's tested and works. You can use it for free (same license as SFML) It's on the wiki: https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite
Give it a try if you want :)

Camble

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Inherited sf::Drawable with SFML 2.0
« Reply #5 on: January 19, 2013, 08:22:54 pm »
What do you mean by same syntax?

If I store the sprites in an array declared in main(), as opposed to within my SpriteSheet class, it works fine.

ie. window.draw(*pSprites[0][0]);

So yes, [0 ][0 ] contains a valid sprite.

My error:
Program received signal SIGSEGV, Segmentation fault.
In sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&) () (C:\Windows\sfml-graphics-2.dll)
 

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Inherited sf::Drawable crashing
« Reply #6 on: January 21, 2013, 12:40:21 am »
Note that your class constructor uses a sf::texture* and it doesn't check whether the texture that is being pointed at exists or not, try removing the line that sets the texture and if it doesn't crash any further then it was probably a pointer to an invalid texture.

Also, something that may sound like a dumb beginner mistake (suggested as a way of removing any possible error) but its still worth checking: Are the values rows and columns below 16? If they were higher or not even initialized (the compiler just puts some random number when you don't initialize a numerical variable) then the cause for the SIGSEV would be obvious.
« Last Edit: January 21, 2013, 12:44:26 am by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Inherited sf::Drawable with SFML 2.0
« Reply #7 on: January 21, 2013, 08:36:44 am »
Well the array was really only for testing purposes, but I'll give it a go.
Even for testing, STL containers and automatic memory are the better choice ;)

For example, the index check mentioned by masskiller would be included (in Debug mode).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Camble

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Inherited sf::Drawable crashing
« Reply #8 on: January 22, 2013, 12:46:35 am »
I replaced standard pointers with unique_ptr, which I was unaware even existed until now, and the program still crashed. I think the issue is that I'm running Windows 7 (64bit) and compiling with MinGW. I've just finished testing the same code under a Windows XP (32bit) VM and it runs absolutely fine.

Thanks for the pointers. No pun intended. I have a fair amount of catching up to do!
« Last Edit: January 22, 2013, 12:48:29 am by Camble »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: Inherited sf::Drawable crashing
« Reply #9 on: January 22, 2013, 01:00:06 am »
I think the issue is that I'm running Windows 7 (64bit) and compiling with MinGW.
This shouldn't be an issue. I've been using Windows 7 x64 and MinGW without problems and I'm now using Windows 8 with MinGW without problems...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Camble

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Inherited sf::Drawable crashing
« Reply #10 on: January 22, 2013, 09:06:11 pm »
I actually to test this a little more. It was working, but am getting various segfaults even under 32bit. I may have jumped to conclusions.
« Last Edit: January 22, 2013, 09:23:10 pm by Camble »

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Inherited sf::Drawable crashing
« Reply #11 on: January 22, 2013, 09:09:23 pm »
Quote
I actually to test this a little more. It was working, but am getting various segfaults even under 32bit. I may have jumped to conclusions.

It was to be expected I guess, note that while a 32bit computer cannot run (sometimes even install) a 64bit program this isn't particularly true backwards. A 64bit computer can perfectly run 32 bit programs.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Camble

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Inherited sf::Drawable crashing
« Reply #12 on: January 22, 2013, 09:23:33 pm »
Debugging now shows up sf::VideoMode in D:\developpement\sfml-master\src\SFML\Window\VideoMode.cpp:50, but I have SFML under C:\SFML-2.0

I've tried both the release candidate and the latest snapshot (120).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: Inherited sf::Drawable crashing
« Reply #13 on: January 22, 2013, 09:27:05 pm »
If you create a complete and minimal example, we could actually test it on our ends.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Camble

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Inherited sf::Drawable crashing
« Reply #14 on: January 22, 2013, 09:28:31 pm »
I'll do that now, but it doesn't seem to matter what I do. I've taken working examples from elsewhere and get the same issue.