detectivecalcite
detectivecalcite

Reputation: 83

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

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?

Upvotes: 6

Views: 4502

Answers (2)

Rolandas Ulevicius
Rolandas Ulevicius

Reputation: 304

Ive personally had problems with SDL_TEXTURE that caused a memory leak while the program was running and the display of pictures just stopped after the program leaked about 2gb of ram when normally it uses 37 mb of ram.

SDL_DestroyTexture(texture);

Just called this afer every time i used to display a different picture with renderer and the memory leak was gone

Upvotes: 0

keltar
keltar

Reputation: 18399

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.

Upvotes: 2

Related Questions