SFML community forums

Help => Graphics => Topic started by: BobTheFish on February 28, 2010, 06:59:29 am

Title: Confusion concerning sf::Image pointers
Post by: BobTheFish on February 28, 2010, 06:59:29 am
My current project is crashing at exit, complaining about corruption of the heap.
It seems to me that the problem is my image objects are being deleted a second time on exit! The crashing/heap problem goes away if I just remove the line where I manually delete my image.

Here's some code, try it for yourself (I'm using SFML2 revision 1429, Visual C++ 2008 Express). Put a breakpoint in Image::~Image in Image.cpp, and run this code. It breaks twice: once on my delete and another at the end of the program.

Code: [Select]
#include <SFML/Graphics.hpp>

sf::Image* Image;

int main()
{
Image = new sf::Image;
delete Image;

    return 0;
}


Is this standard behaviour for SFML?
Title: Confusion concerning sf::Image pointers
Post by: dunce on February 28, 2010, 08:18:49 am
Don't use global pointer variables. Try to place sf::Image* Image; declaration inside main().
Title: Confusion concerning sf::Image pointers
Post by: Laurent on February 28, 2010, 11:21:47 am
Make sure that you use SFML debug libraries in debug mode (and release ones in release mode).

Quote
Don't use global pointer variables. Try to place sf::Image* Image; declaration inside main().

Well, what is dangerous is executing something outside main, which can happen in constructors and destructors of global objects. But declaring a raw pointer doesn't do anything, so it's perfectly safe.
Title: Confusion concerning sf::Image pointers
Post by: BobTheFish on February 28, 2010, 12:11:18 pm
Quote from: "Laurent"
Make sure that you use SFML debug libraries in debug mode (and release ones in release mode).

Yep made sure of that :)

I guess my first hypothesis was wrong, but I'm having a hard time finding what's causing the problem.

A summary of what I know so far:
I have a number of objects, each has a sprite using the same image. If I manually delete either an object or the image, there  are no problems. If I do both, however, I get heap issues.

I tried to replicate this with a simple example, but it doesn't exhibit the problem.
Code: [Select]
#include <SFML/Graphics.hpp>

class testclass
{
public:
testclass(sf::Image& i)
{
sprite.SetImage(i);
}

sf::Sprite sprite;
};

int main()
{
sf::Image* Image;
Image = new sf::Image;
Image->LoadFromFile("test.png");
testclass* Sprite = new testclass(*Image);
testclass* Sprite2 = new testclass(*Image);
delete Sprite;
delete Sprite2;
delete Image;

    return 0;
}


The only thing I can think it must be is that somehow the destructor for my objects is corrupting the heap so causing sf::~Resource to crash. I've searched in my code and fiddled for hours on end and I'm coming up with nothing.
What's the best way to find out where a heap corruption might be caused?
Title: Confusion concerning sf::Image pointers
Post by: BobTheFish on February 28, 2010, 02:00:25 pm
Hmm it's magically gone away, and nothing is different.
I hate it when that happens because it might come back and until it does (if ever) we'll never know what the problem is!

Well I couldn't find any good reason for it happening so hopefully it was just a random glitch that goes away...

So whatever happens it wasn't SFML's fault. Case closed. Thanks for your help anyway.
Title: Confusion concerning sf::Image pointers
Post by: panithadrum on February 28, 2010, 08:05:32 pm
Were you using dynamic SFML?
Title: Confusion concerning sf::Image pointers
Post by: BobTheFish on February 28, 2010, 10:11:27 pm
No, static.
Title: Confusion concerning sf::Image pointers
Post by: bananu7 on December 08, 2010, 11:49:38 pm
Sorry for posting to old thread, but I've recently got the same error. Heap crash, using dynamically linked sfml-*-d.dll

//EDIT: ok, some linking settings were wrong. I still think that linking sfml is like a roulette...
Title: Confusion concerning sf::Image pointers
Post by: Laurent on December 09, 2010, 08:07:15 am
Quote
ok, some linking settings were wrong. I still think that linking sfml is like a roulette...

Can you tell me more about that please?