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

Author Topic: Confusion concerning sf::Image pointers  (Read 3355 times)

0 Members and 1 Guest are viewing this topic.

BobTheFish

  • Newbie
  • *
  • Posts: 17
    • View Profile
Confusion concerning sf::Image pointers
« 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?

dunce

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Confusion concerning sf::Image pointers
« Reply #1 on: February 28, 2010, 08:18:49 am »
Don't use global pointer variables. Try to place sf::Image* Image; declaration inside main().

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Confusion concerning sf::Image pointers
« Reply #2 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.
Laurent Gomila - SFML developer

BobTheFish

  • Newbie
  • *
  • Posts: 17
    • View Profile
Confusion concerning sf::Image pointers
« Reply #3 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?

BobTheFish

  • Newbie
  • *
  • Posts: 17
    • View Profile
Confusion concerning sf::Image pointers
« Reply #4 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.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Confusion concerning sf::Image pointers
« Reply #5 on: February 28, 2010, 08:05:32 pm »
Were you using dynamic SFML?

BobTheFish

  • Newbie
  • *
  • Posts: 17
    • View Profile
Confusion concerning sf::Image pointers
« Reply #6 on: February 28, 2010, 10:11:27 pm »
No, static.

bananu7

  • Newbie
  • *
  • Posts: 25
    • View Profile
Confusion concerning sf::Image pointers
« Reply #7 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...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Confusion concerning sf::Image pointers
« Reply #8 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?
Laurent Gomila - SFML developer

 

anything