Dynamic memory allocation and SDL_Surfaces

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). 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). If anyone has any thoughts, they
would be greatly appreciated. If it matters, I’m coding in C (MS C++ 6.0
Professional). Thanks.

Regards,

Chris Dickinson, PhD
Department of Psychology
University of Delaware

Dunno if I can wrap my head around what all you’re doing. You say you
have an array of SDL_Surfaces. Are you talking about actual surfaces,
which are structures which themselves hold pointers to allocated spaces?
Or is the first array an array of pointers to SDL_Surfaces?

Lilith

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). 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). If anyone has any
thoughts, they

would be greatly appreciated. If it matters, I’m coding in C (MS C++
6.0>>> On 9/5/2007 at 3:29 PM, Chris Dickinson wrote:
Professional). Thanks.

Regards,

Chris Dickinson, PhD
Department of Psychology
University of Delaware


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

perhaps you could post an example, that was hard to follow.

mattOn Wed, 5 Sep 2007 20:29:32 +0000 (UTC) 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). 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). If anyone has
any thoughts, they would be greatly appreciated. If it matters, I’m
coding in C (MS C++ 6.0 Professional). Thanks.

Regards,

Chris Dickinson, PhD
Department of Psychology
University of Delaware


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Wednesday 05 September 2007 wrote Chris Dickinson:

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). 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). If anyone
has any thoughts, they would be greatly appreciated. If it matters, I’m
coding in C (MS C++ 6.0 Professional). Thanks.
For each malloc you should have exactely one free to avoid memory leaks. So,
yes, you should free(surface2).–
Christer

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

UliOn Wednesday 05 September 2007 22:29:32 Chris Dickinson wrote: