# Do I need to delete a texture after calling SDL_DestroyTexture()?

Let's suppose I have this texture:

SDL_Texture* txt = IMG_LoadTexture(renderer, "path");
/* various code */
SDL_DestroyTexture(txt);
delete txt; // <- Is this part really necessary? Doesn't SDL_DestroyTexture() does it already?


Since there's not a lot of documentation about SDL, I haven't found an answer to my question on my own.

No.

SDL2 is a C library, and since C has no delete keyword, it wouldn't make sense here.

C allocates memory using malloc and friends, but usually C libraries take the approach of managing the memory for you, so if there are functions like SDL_DestroyTexture, you can assume that they also free the allocated memory.

• It might help mentioning that resource creation and resource destruction functions normally come in pairs, and mentioning the common ones. – Lars Viklund Nov 6 '15 at 13:09
SDL_DestroyTexture(txt);


should clean up and then free the memory used by the texture. However you will be left with a dangling pointer so be sure to deal with that.

txt = NULL //if using C
txt = nullptr //if using C++


(Unless you're really sure the variable won't ever be used again before you leave the scope)

Without doing this if the pointer variable is used again before being reassigned it will likely cause a crash or a bug.

• I'm sorry, even if your answer isn't wrong, it isn't really relevant to the question because I asked something a little bit different... – Xriuk May 20 '16 at 17:48