3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ It might help mentioning that resource creation and resource destruction functions normally come in pairs, and mentioning the common ones. \$\endgroup\$ – Lars Viklund Nov 6 '15 at 13:09
1
\$\begingroup\$
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.

\$\endgroup\$
  • \$\begingroup\$ 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... \$\endgroup\$ – Xriuk May 20 '16 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.