Error SDL_FreeSurface()


#1

Hi,
I’ve an Image class. My destructor is

Image::~Image
{
SDL_FreeSurface(mp_s_img);
}

in my main.cc I’ve

while(!game.Keyboard())
{

when Keyboard return 1 than my program close, and the destructor for all
my objects is call.
But if I use the destructor in this mode, the program return to me

Fatal signal: Segmentation Fault (SDL Parachute Deployed)

why?


#2

Hi,
I’ve an Image class. My destructor is

Image::~Image
{
SDL_FreeSurface(mp_s_img);
}

when Keyboard return 1 than my program close, and the destructor for all
my objects is call.
But if I use the destructor in this mode, the program return to me

Fatal signal: Segmentation Fault (SDL Parachute Deployed)

why?

I don’t know C++, but are you certain that the surface hasn’t ALREADY
been freed by SDL before your destructor got called?

Maybe check to see if “mp_s_img” is valid before trying to free it. :slight_smile:

-bill!On Fri, May 21, 2004 at 10:58:47PM +0200, NighTiger wrote:


#3

NighTiger <nightiger gameprog.it> writes:

Hi,
I’ve an Image class. My destructor is

Image::~Image
{
SDL_FreeSurface(mp_s_img);
}

in my main.cc I’ve

Hard to say with the short code snippet. Is mp_s_img initialized? Does it
get freed anywhere else, and if so you should set it to NULL and have the
destructor check for NULL before calling SDL_FreeSurface().

One other possibility is that something is still using the surface (has it
locked) when you try and free it. Did you stderr.txt file have anything more
useful than “SDL Parachute deployed”?


#4

There’s not enough information here to really answer your question. But
a few things that might be happening are this.

Are you creating Image instances that aren’t being used?

Image img[3];
img[0].load(“this.png”);
img[1].load(“that.png”);

// stuff

Segfault.

If so, it’s your free call on the uninitalized 3rd member of the array.
This would also apply to things that failed to load, like bad files etc.

A good solution to this is to set your mp_s_img = 0 in the constructor,
and have your destructor look something like:

if (mp_s_img) SDL_FreeSurface(mp_s_img);

The other common problem you might be having is you’re either
initalizeing an Image class with the result of SDL_SetVideo, or you’ve
derived a Video class from Image. In either case, the deconstructor is
probably trying to free the screen surface, which you’re not supposed to
do. If that’s the case, introduce a concept of ownership into your
Image class. An internal bool that’s always true in Image, but you can
set to false in a derived class, and will be checked before freeing the
surface in the destructor.On Fri, 2004-05-21 at 16:58, NighTiger wrote:

Hi,
I’ve an Image class. My destructor is

Image::~Image
{
SDL_FreeSurface(mp_s_img);
}

in my main.cc I’ve

while(!game.Keyboard())
{

when Keyboard return 1 than my program close, and the destructor for all
my objects is call.
But if I use the destructor in this mode, the program return to me

Fatal signal: Segmentation Fault (SDL Parachute Deployed)

why?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


#5

There’s not enough information here to really answer your question. But
a few things that might be happening are this.

Are you creating Image instances that aren’t being used?

Image img[3];
img[0].load(“this.png”);
img[1].load(“that.png”);

// stuff

Segfault.

If so, it’s your free call on the uninitalized 3rd member of the array.
This would also apply to things that failed to load, like bad files etc.

A good solution to this is to set your mp_s_img = 0 in the constructor,
and have your destructor look something like:

if (mp_s_img) SDL_FreeSurface(mp_s_img);

The other common problem you might be having is you’re either
initalizeing an Image class with the result of SDL_SetVideo, or you’ve
derived a Video class from Image. In either case, the deconstructor is
probably trying to free the screen surface, which you’re not supposed to
do. If that’s the case, introduce a concept of ownership into your
Image class. An internal bool that’s always true in Image, but you can
set to false in a derived class, and will be checked before freeing the
surface in the destructor.

Tnx Jimmy and tnx to everybody for your help.
Now the game close perfectly> Date: Fri, 21 May 2004 18:23:38 -0400

From: Jimmy
Subject: Re: [SDL] Error SDL_FreeSurface()
To: “@NighTiger;”


#6

I have suffered the problem only with images after making:

windows_surface=SDL_GetWindowSurface(window);

I have look at SDL_video.h and it returns a pointer, I suposse it points to somewhere inside SDL_window class so I think if you make

SDL_DestroyWindow(window);

it is not needed to make
SDL_FreeSurface(windows_surface);


#7

dude, this is 14years old O_O

anyway, as https://wiki.libsdl.org/SDL_GetWindowSurface#Remarks says:

This surface will be freed when the window is destroyed. Do not free this surface.