SFML community forums

Help => Graphics => Topic started by: Ankou on March 25, 2008, 11:04:53 pm

Title: Delete sf::Image in destructor?
Post by: Ankou on March 25, 2008, 11:04:53 pm
Hi
I have an sf::image created with new in the constructorcode of my object.
For testing purposes I have created an std::string with new at the same location.
When I'm deleting the sf::image in the destructor I get an access violation(I'm not using this object in any place I just created and deleted it)
It's not a problem with my class, because I can delete the string without problems.
So how can I delete the sf::Image without an access violation?(I have the same problem with an Smart Pointer(boost::shared_ptr) or when not using a pointer at all.
Title: Delete sf::Image in destructor?
Post by: Laurent on March 26, 2008, 02:40:03 am
Quote
I have an sf::image created with new in the constructorcode of my object.
For testing purposes I have created an std::string with new at the same location.

You don't need these dynamic allocations at all.
Allocating a std::string with new is kind of horrible, as the main purpose of this class (and standard containers in general) is to hide the memory management ;)

I don't think there's something wrong with sf::Image destruction, you're the first one to report such issue, and I can't see what could crash in this class.
Title: Delete sf::Image in destructor?
Post by: Ankou on March 26, 2008, 03:07:24 am
It was just for testing purposes first I had a Smart Pointer.
The std::string was just to test if it is only with sf::Image or with every object and yes, it seems only to happen with sf::Images(I also added another image to test if I do any stuff with it)
But yes it doesn't happen in general, in another testing class I could delete it without problems.
But sf::image is still the only case, so it has to do something with it, I just don't know what.
So I asked if anybody knows one possible reason for this behaviour...
It's late, I'm tired please excuse my horrible english...
Title: Delete sf::Image in destructor?
Post by: Laurent on March 26, 2008, 04:17:27 am
Could you show us the code related to the destruction of your class ? Is it done at global program exit (is your object static / global ?) ?
Title: Delete sf::Image in destructor?
Post by: Ankou on March 26, 2008, 01:30:41 pm
yes that's it. I found and simplified the problem.
The following Code works:
Code: [Select]

class A
  {
  public:
    sf::Image* test;
    A() : test(NULL)
      {
      test = new sf::Image();
      }
    ~A()
      {
      delete test;
      test = NULL;
      }
  };
int main()
  {
  boost::shared_ptr<A> a = boost::shared_ptr<A>(new A());
  }


This following does not!:
Code: [Select]

class A
  {
  public:
    sf::Image* test;
    A() : test(NULL)
      {
      test = new sf::Image();
      }
    ~A()
      {
      delete test;
      test = NULL;
      }
  };
boost::shared_ptr<A> a;
int main()
  {
  a = boost::shared_ptr<A>(new A());
  }


But it shouldn't be a problem because I only use global variables for testing so I have just to remove the test and implement the real code and it should work
Title: Delete sf::Image in destructor?
Post by: Laurent on March 27, 2008, 01:51:29 am
It shouldn't crash however, as images are supposed to be aware of the destruction of the OpenGL context.

Is there any error message on the standard output ?
Title: Delete sf::Image in destructor?
Post by: Ankou on April 04, 2008, 01:47:57 am
sorry that it took so long to answer:
no, there's no Output.

Don't you have the same problem, when you run my Code?(I'm using Visual C++ 2008)
Title: Delete sf::Image in destructor?
Post by: Laurent on April 04, 2008, 03:28:23 am
No, sorry. I'll try to test it as soon as posible.