TheMrZZ
TheMrZZ

Reputation: 308

Should I use SDL_Quit() even after a failed initialisation?

When using SDL2.0, is it reasonable to stop the program without using SDL_Quit() if SDL could not create the window or the renderer ?

For example, can I write (after the SDL initialisation, the window and the renderer creation):

if (!renderer)
{
    fprintf("Error while creating the renderer: %s\n", SDL_GetError());
    return -1;
}

SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;

Or should I write :

if (!renderer)
{
    fprintf("Error while creating the renderer: %s\n", SDL_GetError());
    SDL_DestroyWindow(window);
    SDL_Quit();
    return -1;
}

SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;

I think the second choice is better, but I'm not sure that it is really useful to close the SDL and to destroy the window even when the renderer wasn't created.

Upvotes: 3

Views: 1741

Answers (1)

Nelfeal
Nelfeal

Reputation: 13269

From the SDL Wiki :

You should call it upon all exit conditions.

So, assuming you called SDL_Init before, which is required for using any other SDL function, you should call SDL_Quit no matter what.

Also, from the same page :

It is safe to call this function even in the case of errors in initialization.

That means you don't even have to worry about being wrong for calling it.

For convenience, you can use SDL_Quit with atexit, but that is more of a matter of taste.

Upvotes: 6

Related Questions