[SDL] dynamic memory allocation and SDL_Surfaces

Ulrich Eckhardt doomster at knuut.de
Thu Sep 6 22:19:26 PDT 2007


On Wednesday 05 September 2007 22:29:32 Chris Dickinson wrote:
> I have a question about freeing pointers to surfaces - I'm trying to figure
> out whether my program might have a memory leak.  In one function, I use
> malloc () to create a pointer to an array of SDL_Surfaces. I then load
> various images onto the surfaces and pass a pointer to the array to a
> second function.  In the second function, I use malloc() to create a second
> array of pointers to SDL_Surfaces, and I use this array to access the
> surfaces passed to the function (I know that on the surface this sounds
> odd, but within the program there is a good reason for doing this).  After
> the called function returns, I free each of the surfaces in the array
> [i.e., for (i=0; i<nSurfaces; i++) SDL_FreeSurface(surface1[i])] and then
> free the pointer [free(surface1)].  If I do this, but don't attempt to free
> the pointers created in the second function, the program works as it
> should, and I don't see any obvious evidence of memory leaks.  If in the
> function that is called I try to free the pointers to the individual
> surfaces that were allocated with the call to malloc() [i.e., for (i=0;
> i<nSurfaces; i++) SDL_FreeSurface(surface2[i])], I get a seg fault (which
> is expected).  If, however, I simply free the pointer [i.e., free
> (surface2)], I don't get a seg fault.  My question is whether I need to
> free the pointer of SDL_Surfaces allocated in the called function or not
> (and if not freeing it will result in a memory leak).

You can split up your question in two parts:
1. managing several SDL_Surfaces
2. managing an array of pointers

For both parts, the principle below applies. Note that releasing an array of 
pointers with free() does not touch the pointers themselves. The objects they 
point to have to be handled independently.

> I know that if I were passing a pointer to a single surface and then
> accessing it with another pointer, freeing both would cause a seg fault
> (only the pointer to the original surface should be freed - the one that
> was passed).

This is wrong or at least misleading, I think you are missing the distinction 
between a pointer and the pointer's value (there's a third thing, the value 
that the pointer points to, but that is not important here).

malloc() returns a value, which must eventually be passed to free(), the same 
principle applies to the functions creating and destroying SDL_Surfaces. Now, 
the _pointer_ where the returnvalue of the allocation function is initially 
stored is in no way special. What matters is that the _value_ is passed 
exactly once to the deallocation function. Do it twice and you get 
segementation faults or similar errors, omit it and you get memory leaks.

> If it matters, I'm coding in C (MS C++ 6.0 Professional).

This compiler/IDE is almost ten years old and isn't even supported anymore by 
its vendor. You should upgrade. If you don't want to spend money, get DevC++ 
or the free-as-in-beer Express Edition.

cheers

Uli


More information about the SDL mailing list