Freeing SDL_Surfaces

This is beginning to drive me nuts… Can someone please help,

I have a bunch of surfaces in a array… so,

SDL_Surface *shape[50];

When I load the game’s graphics I load each one into a seperate array
index…

shape[0] = loadImage(“image1.png”);
shape[1] = loadImage(“image2.png”);
shape[2] = loadImage(“image3.png”);
etc…

This is all fine. But since I’m reusing the array for different parts of the
game, I want to free the data first,

for (int i = 0 ; i < 50 ; i++)
{
if (shape[i] != NULL)
SDL_FreeSurface(shape[i]);
}

Then I want to load in the new graphics (like I do in the bit above). The
problem is that as I’m looping through the shapes to remove them. It seg
faults. Any ideas?

for (int i = 0 ; i < 50 ; i++)
{
if (shape[i] != NULL)
SDL_FreeSurface(shape[i]);

You may want to set shape[i] to NULL here, too.

Then I want to load in the new graphics (like I do in the bit
above). The problem is that as I’m looping through the shapes
to remove them. It seg faults. Any ideas?

Yes, printf. Add a bunch of printf’s here-and-there to pinpoint to
place where your program is crashing. Right now, the reason for the
crash could be anything, because you don’t know the specific
instance where it crashes. Maybe you forgot to load a surface at a
particular index and you are now trying to free bogus information.
Who knows? Add some debug output and you know. You could also use a
debugger, of course, but Real Programmers use printf ;-)–
Matthijs Hollemans
www.allyoursoftware.com

printf’s with fflush’s :^)

printf(“I am here #1\n”); fflush(stdout);

/* do something that may segfault */

printf(“I am here #2\n”); fflush(stdout);

/* do something simple */

printf(“I am here #3\n”); fflush(stdout);

And then watch when it gets to “I am here #2” ;^)

(It’s always the most benign-looking code that crashes)

-bill!On Fri, Nov 29, 2002 at 10:41:45AM +0100, Matthijs Hollemans wrote:

Yes, printf. Add a bunch of printf’s here-and-there to pinpoint to
place where your program is crashing.

You should initialize each array cell to NULL after creating it. I am not an expert but compilers don’t always initialize arrays or variables. So if you don’t load 50 images the first time you may try to free a surface that hasn’t been created.

Julien> This is beginning to drive me nuts… Can someone please help,

I have a bunch of surfaces in a array… so,

SDL_Surface *shape[50];

When I load the game’s graphics I load each one into a seperate array
index…

shape[0] = loadImage(“image1.png”);
shape[1] = loadImage(“image2.png”);
shape[2] = loadImage(“image3.png”);
etc…

This is all fine. But since I’m reusing the array for different parts of the
game, I want to free the data first,

for (int i = 0 ; i < 50 ; i++)
{
if (shape[i] != NULL)
SDL_FreeSurface(shape[i]);
}

Then I want to load in the new graphics (like I do in the bit above). The
problem is that as I’m looping through the shapes to remove them. It seg
faults. Any ideas?


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

you can also link with efence and/or simply export MALLOC_CHECK_ to 1 or 2

Wagner FredericOn Fri, Nov 29, 2002 at 10:41:45AM +0100, Matthijs Hollemans wrote:

Yes, printf. Add a bunch of printf’s here-and-there to pinpoint to
place where your program is crashing. Right now, the reason for the
crash could be anything, because you don’t know the specific
instance where it crashes. Maybe you forgot to load a surface at a
particular index and you are now trying to free bogus information.
Who knows? Add some debug output and you know. You could also use a
debugger, of course, but Real Programmers use printf :wink:

I think your question has been answered: zero it out ahead of time,
memset(shape, 0, sizeof(shape));
ahead of time and NULL it out after freeing it.On Fri, Nov 29, 2002 at 08:59:25AM -0000, Sweeney, Steven (FNB) wrote:

This is all fine. But since I’m reusing the array for different parts of the
game, I want to free the data first,

for (int i = 0 ; i < 50 ; i++)
{
if (shape[i] != NULL)
SDL_FreeSurface(shape[i]);
}

I’d suggest writing this as:

for (int i = 0 ; i < 50 ; i++)
{
SDL_FreeSurface(shape[i]);
shape[i] = NULL;
}

That is, it’s OK to send NULL to most free()-style calls, and good practice
to simply do it (so the test is always done in one place–in the free),
instead of testing on each call and putting extra conditionals all over
the place. ref:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.7

(Only slightly off-topic, I think; I think it’s worth pointing out, since
SDL_FreeSurface does the same thing that free() and delete do.)


Glenn Maynard

Bill Kendrick wrote:> On Fri, Nov 29, 2002 at 10:41:45AM +0100, Matthijs Hollemans wrote:

Yes, printf. Add a bunch of printf’s here-and-there to pinpoint to
place where your program is crashing.

printf’s with fflush’s :^)

printf(“I am here #1\n”); fflush(stdout);

\n flushes the buffer anyway, at least if you havn’t messed with sdtio
lib default settings.

Thanks guys… Setting and checking for NULLs worked.

Cheers :slight_smile:

Do you initialize shape[] before loading images? Perhaps the values of
shape[x] is non-NULL initially. So, it failes when SDL_FreeSurface() is
called.From: sweenes@fnb.co.uk (Steven Sweeney)
Subject: [SDL] Freeing SDL_Surfaces
Date: Fri, 29 Nov 2002 08:59:25 -0000

This is beginning to drive me nuts… Can someone please help,

I have a bunch of surfaces in a array… so,

SDL_Surface *shape[50];

When I load the game’s graphics I load each one into a seperate array
index…

shape[0] = loadImage(“image1.png”);
shape[1] = loadImage(“image2.png”);
shape[2] = loadImage(“image3.png”);
etc…

This is all fine. But since I’m reusing the array for different parts of the
game, I want to free the data first,

for (int i = 0 ; i < 50 ; i++)
{
if (shape[i] != NULL)
SDL_FreeSurface(shape[i]);
}

Then I want to load in the new graphics (like I do in the bit above). The
problem is that as I’m looping through the shapes to remove them. It seg
faults. Any ideas?