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

Author Topic: Image Manager Destruction (Solved)  (Read 2905 times)

0 Members and 1 Guest are viewing this topic.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Image Manager Destruction (Solved)
« on: January 01, 2012, 12:19:41 am »
Hello,

I'm using an image manager based on the following file:
http://www.sfml-dev.org/wiki/fr/sources/imagemanager

I'm using the manager as a global variable as it makes things easier, although I'm not sure if I should go with this or a singleton.

But that aside, I'm having some trouble trying to understand why the program simply crashes when exiting. For some reason the call stack points to the destructor on the line:
Code: [Select]
delete it->second;

The application runs fine, but when exiting, it crashes. Although It only happens if the manager is a global. If it's inside the main then there's no problem.

What is causing this? What can I do to avoid this?
Also, I'm compiling with Visual Studio 2008.
Thanks in advance and happy new year  :)

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Image Manager Destruction (Solved)
« Reply #1 on: January 01, 2012, 03:09:59 am »
Without seeing more of your code it's hard to say. A guess would be that you're destroying the manage more than once.

Quote
although I'm not sure if I should go with this or a singleton.


Monostate. :)

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Image Manager Destruction (Solved)
« Reply #2 on: January 01, 2012, 03:58:29 am »
Darn my mistake, forgot minimal code.
Here it is:
Code: [Select]
#include <SFML\Graphics.hpp>
#include "ImageManager.hpp"

ImageManager imgmng;

int main()
{
sf::RenderWindow App(sf::VideoMode(560,270,32),"Error");
imgmng.GetImage("any.png");
return 0;
}

Just this reproduces the error. On the image manager that i linked above I just replaced sf::Image with sf::texture for SFML 2.0 and included these headers that were missing:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <map>
#include <string>


When closing, the call stack points to delete it->second; on the destructor.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Image Manager Destruction (Solved)
« Reply #3 on: January 02, 2012, 03:13:32 am »
I actually didn't know what a monostate was so I researched a bit and tried using it (defined it as static) but the problem persists.
I did:

Code: [Select]
class Managers
{
private:
static ImageManager imgmng;
};

//outside the class
ImageManager Manager::imgmng = ImageManager();

This works but the same error occurs when exiting. Did I do this wrong?

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Image Manager Destruction (Solved)
« Reply #4 on: January 02, 2012, 08:11:39 am »
I guess it's fixed. I don't know if this is a good practice but... Just added this on the destructor before delete:
Code: [Select]
it->second = NULL;
delete it->second;

And it's working like a peach as singleton, global and monostate object...  :?

Thanks for the help,
Regards.

EDIT: Actually I just realized that deleting a null pointer doesn't actually free the allocated space. Darn. Any more ideas?  :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Image Manager Destruction (Solved)
« Reply #5 on: January 02, 2012, 08:19:02 am »
Now you delete a null pointer instead of the actual memory address that contains your variable. So you just turned your crash into a memory leak. Not a very good "solution" ;)

I didn't read the whole thread, but I assume that your problem is that you destroy images at global exit. Don't do that, the behaviour is undefined since SFML also uses globals, and they might be destroyed before your own globals.

A simple solution is to add a ImageManager::cleanup() function that you call just before leaving main().
Laurent Gomila - SFML developer

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Image Manager Destruction (Solved)
« Reply #6 on: January 02, 2012, 10:10:18 pm »
Thanks! It works like a charm!  :D