Reputation: 99
I am doing this lazyfoo SDL tutorial right now.
The render class they using has some image to texture function which is using SDL_Surface*
and SDL_Texture
.
At the end of the function they "free" the created surface by calling SDL_FreeSurface()
.
Now, I am wondering:
SDL_DestroyTexture
? bool Tile::loadTexture(const char* path){
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = IMG_Load(path);
//...some code
Texture = newTexture;
SDL_FreeSurface(loadedSurface);
return Texture != NULL;}
Upvotes: 1
Views: 3722
Reputation: 13269
Why exactly i have to free the Surface at all (variables are local?)?
The pointer loadedSurface
is local. The actual surface isn't: there is something similar to malloc
inside of IMG_Load
. The same way you use free
on memory allocated with malloc
, you use SDL_FreeSurface
on surfaces allocated with IMG_Load
(or SDL_CreateRGBSurface
and so on).
Why its ok to let the created Texture be without calling SDL_DestroyTexture?
SDL_DestroyTexture
is called, inside of LTexture::free
, which is called by the destructor of LTexture
. So SDL_DestroyTexture
is pretty much guaranteed to be called at some point if loadFromFile
was called.
What exacty does it mean when i destroy a texture or free a surface?
It means the same thing as using free
on memory allocated with malloc
, or using delete
on memory allocated with new
, or calling std::unique_ptr::reset
(without argument), and so on. Each variant does something sligthly different. If you want to know what exactly differs between SDL_DestroyTexture
, SDL_FreeSurface
, free
, etc, you can look at the source code: SDL is open source and there are quite a few open source implementations of free
out there.
Upvotes: 4