SFML community forums

Help => Graphics => Topic started by: Camble on January 19, 2013, 05:57:36 pm

Title: Inherited sf::Drawable crashing
Post by: Camble 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.
Title: Re: Inherited sf::Drawable with SFML 2.0
Post by: Nexus 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.
Title: Re: Inherited sf::Drawable with SFML 2.0
Post by: Camble on January 19, 2013, 06:23:19 pm
Well the array was really only for testing purposes, but I'll give it a go.
Title: Re: Inherited sf::Drawable with SFML 2.0
Post by: FRex 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?
Title: Re: Inherited sf::Drawable with SFML 2.0
Post by: Foaly 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 :)
Title: Re: Inherited sf::Drawable with SFML 2.0
Post by: Camble 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)
 
Title: Re: Inherited sf::Drawable crashing
Post by: masskiller 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.
Title: Re: Inherited sf::Drawable with SFML 2.0
Post by: Nexus 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).
Title: Re: Inherited sf::Drawable crashing
Post by: Camble 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!
Title: Re: Inherited sf::Drawable crashing
Post by: eXpl0it3r 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...
Title: Re: Inherited sf::Drawable crashing
Post by: Camble 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.
Title: Re: Inherited sf::Drawable crashing
Post by: masskiller 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.
Title: Re: Inherited sf::Drawable crashing
Post by: Camble 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).
Title: Re: Inherited sf::Drawable crashing
Post by: eXpl0it3r on January 22, 2013, 09:27:05 pm
If you create a complete and minimal example, we could actually test it on our ends.
Title: Re: Inherited sf::Drawable crashing
Post by: Camble 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.
Title: Re: Inherited sf::Drawable crashing
Post by: eXpl0it3r on January 22, 2013, 09:31:56 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.
Hmm we haven't asked yet, what should've actually been in the first post anyways:
What exact SFML version are you using or from where did you get it?
What exact MinGW version are you using?

Because if the GCC version >= 4.7.x and you're using the RC builds, then it won't work and you'll have to recompile SFML or use my nightly builds. ;)
Title: Re: Inherited sf::Drawable crashing
Post by: Camble on January 22, 2013, 09:33:53 pm
Even the follow example gives me a segfault when creating an sf::RenderWindow

#include <memory>
#include <SFML/Graphics.hpp>

using namespace std;

int main() {
       
    sf::Event event;
        sf::RenderWindow testWindow(sf::VideoMode(800, 600, 32), "Test");

        while(testWindow.isOpen()) {
                sf::Event event;
                while(testWindow.pollEvent(event)) {
                        if(event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
                                testWindow.close();
                }

                testWindow.clear();

                testWindow.display();
        }

        return 0;
}
 
Title: Re: Inherited sf::Drawable crashing
Post by: Camble on January 22, 2013, 09:35:27 pm
What exact SFML version are you using or from where did you get it?
What exact MinGW version are you using?

I'm using SFML 2.0 RC, from this website.
Latest version of MinGW.

I should add that I've been trying different versions in an attempt to get it working. That's why I created a new VM so I could have a clean slate to test it.

Last week, I had a RenderWindow with sprites displayed. Not any more.
Title: Re: Inherited sf::Drawable crashing
Post by: eXpl0it3r on January 22, 2013, 09:36:48 pm
I'm using SFML 2.0 RC, from this website.
Latest version of MinGW.
... which are incompatible. ;)

Would this info have been included in the first post, you'd have gotten your answer (rebuild SFML) in the second post. :D

In case you don't find my Nightlies: http://sfml.my-gate.net/nightly/
Title: Re: Inherited sf::Drawable crashing
Post by: Camble on January 22, 2013, 09:37:52 pm
... which are incompatible. ;)

*twitch*

That'll be why my code worked fine with the version of MinGW bundled with CodeBlocks. This is the version I used when I first posted though. Since then I've changed and updated so much. I'll revert back to the older version of MinGW and see how I get on.

I think my original issue was my own fault, using low level pointers etc.
Title: Re: Inherited sf::Drawable crashing
Post by: eXpl0it3r on January 22, 2013, 09:41:53 pm
I'll revert back to the older version of MinGW and see how I get on.
There isn't really the need to do so. The problem is not MinGW, it's the old version of SFML, which anyways already misses some bugfixes...

New is always better! :P
Recompile SFML with the latest compiler or use my Nightly Builds and you're fine!
Title: Re: Inherited sf::Drawable crashing
Post by: Camble on January 22, 2013, 09:42:21 pm
2.0 RC is incompatible with MinGW 4.7.x onwards? So the 4.7.1 bundled with CodeBlocks will still cause issues.

I'll recompile SFML.
Title: Re: Inherited sf::Drawable crashing
Post by: eXpl0it3r on January 22, 2013, 09:44:47 pm
2.0 RC is incompatible with MinGW 4.7.x onwards? So the 4.7.1 bundled with CodeBlocks will still cause issues.
Yes, unfortunately... ;)
Title: Re: Inherited sf::Drawable crashing
Post by: Camble on January 22, 2013, 09:46:05 pm
No wonder the errors were inconsistent.

Thanks for the help :)
Title: Re: Inherited sf::Drawable crashing
Post by: Camble on January 22, 2013, 11:46:10 pm
Worked a treat ;D (of course it did ::))

Thanks again for everyone's help. No RTFMs here anyway.