# SDL\_FreeSurface() confusion

**URL:** <https://discourse.libsdl.org/t/sdl-freesurface-confusion/6684>\
**Category:** SDL Development\
**Created:** [May 20, 2002, 4:23pm UTC](https://discourse.libsdl.org/t/sdl-freesurface-confusion/6684 "2002-05-20T16:23:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Thielen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Christopher\_Thielen](https://discourse.libsdl.org/u/Christopher_Thielen)\
**Post date:** [May 20, 2002, 4:23pm UTC](https://discourse.libsdl.org/t/sdl-freesurface-confusion/6684/1 "2002-05-20T16:23:01Z")

</div>

Hello all,  
I’m having difficulty with SDL\_FreeSurface(). In a function, I  
initalize a surface like:

SDL\_Surface \*image;

I later then say,

image = (SDL\_Surface \*)ship-\>hull-\>image;

(where the ship structure contains another structure, hull, which  
defines image as SDL\_Surface \*image)

I then create two SDL\_Rects, set the x,y,w,h values, use  
SDL\_BlitSurface() to blit image, however, because this function is  
called often, I don’t want to create a memory leak, so I should free up  
image, right?

SDL\_FreeSurface(image);

and it crashes right there w/ an SDL Parachute. Perhaps I’m getting  
something with pointers mixed up? Anybody know what’s wrong and/or  
should I provide more code?–  
Chris  
@Christopher_Thielen

---

<div class="post-metadata">

**Author:** ![Kylotan](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/e95f7d/32.png) [@Kylotan](https://discourse.libsdl.org/u/Kylotan)\
**Post date:** [May 20, 2002, 4:40pm UTC](https://discourse.libsdl.org/t/sdl-freesurface-confusion/6684/2 "2002-05-20T16:40:01Z")

</div>

> I’m having difficulty with SDL\_FreeSurface(). In a function, I  
> initalize a surface like:
> 
> SDL\_Surface \*image;

This doesn’t initialise a surface. It just creates a pointer that can  
later point to a surface.

> I then create two SDL\_Rects, set the x,y,w,h values, use  
> SDL\_BlitSurface() to blit image, however, because this function is  
> called often, I don’t want to create a memory leak, so I should free  
> up  
> image, right?

Wrong. You’ve not created an extra image, you just pointed to an  
existing one. So you shouldn’t free it, as you’ve got exactly the same  
number of images as before.From: [chris@luethy.net](mailto:chris@luethy.net) (Christopher Thielen)  
Sent: Tuesday, May 21, 2002 12:41 AM

–  
Kylotan
