Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I make sure to destroy SDL 2.0 objects (renderer, window, textures, etc.) before exiting the program?

Tags:

c++

sdl

sdl-2

This tutorial on SDL 2.0 uses code that returns from main without first destroying any of the resource pointers:

int main(int argc, char** argv){
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
        std::cout << SDL_GetError() << std::endl;
        return 1; 
    }

    window = SDL_CreateWindow("Lesson 2", SDL_WINDOWPOS_CENTERED, 
        SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    if (window == nullptr){
        std::cout << SDL_GetError() << std::endl;
        return 2; //this
    }
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED 
        | SDL_RENDERER_PRESENTVSYNC);
    if (renderer == nullptr){
        std::cout << SDL_GetError() << std::endl;
        return 3; //and this too
    }

Should I tell my terminate function to DestroyRenderer, DestroyWindow, DestroyTexture, etc. before exiting?

like image 866
detectivecalcite Avatar asked Nov 01 '22 14:11

detectivecalcite


1 Answers

Same question as 'should i free memory that i've allocated before quitting a program'. Yes, if there is no bugs in SDL/X11/GL/etc. finalize code - all will be freed anyway. But i see no reasons why you shouldn't want to do that yourself.

Of course if you crash rather then exit - there is a good chance some things wouldn't be done and e.g. you wouldn't return display to native desktop resolution.

like image 174
keltar Avatar answered Nov 15 '22 03:11

keltar