Search code examples
sdlsdl-2

Why shouldn't I omit SDL_Quit() or SDL_DestroyRenderer() or Mix_Quit() etc?


My basic program works and closes just fine without those or others like SDL_DestroyWindow() or Mix_CloseAudio() on Microsoft Windows.

But if I still shouldn't omit them, is it only because of the memory leaks?

Or am I leaving some channels or other things opened and things undone on the audio or video?

Or something else?


Solution

  • If you only create single renderer in your application (i.e. you only need to delete it once your program finishes) and can rely on your OS to clean things up - no leaks are possible as all of your process's memory will be dropped by OS when it kills your process. Technically you can leave any number of memory allocations, network sockets, open files, etc. - and OS will clean it up (not that it is recommended though). There can be some cases when some finalisation is required - e.g. your fullscreen application disables screensaver, you probably need to re-enable it.

    On some OSes it may be required to finish queued rendering before quitting or otherwise graphics driver may collapse. That is very specific and generally not found on PC.

    SDL_Quit deletes active windows and renderers, closes audio, etc., so that alone should be enough.

    Main goal to free things manually is better readability, as it provides clear lifitime scope - after init some resource is available, and after free it no longer is - and doesn't require knowledge of SDL internals.