luke sim
luke sim

Reputation: 21

Memory Leak with SDL_SetRenderDrawColor when changing active window

I create a simple window with SDL like this:

int main(void)
{
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        printf("SDL_Init Error: %s\n", SDL_GetError());
        return 1;
    }
    SDL_Window* window = SDL_CreateWindow("Hello SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
    
    if (window == NULL) {
        printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    if (renderer == NULL) {
        printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
        return 1;
    }
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // that's where the issue seems to come from
    
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    
    bool stop=false;
    SDL_Event event;
    
    while (!stop){
        if (SDL_PollEvent(&event)) {
            if (event.type==SDL_QUIT) {
                stop=true;
            }
        }
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    IMG_Quit();
    SDL_Quit();

    return 0;
}

when I run this code without changing active window, everything work fine. But if I change active window before closing the window (with SDL_QUIT event) then I got a 6957 bytes memory leak.

I have investigated and found out that it came from the SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); Indeed, when removed, there are no remaining memory leaks

I'm running a Linux Debian 12 with gcc and libsdl2-dev version 2.26.5 I found these memory leaks using AddressSanitizer

What should I do to avoid these memory leaks ?

Upvotes: 2

Views: 32

Answers (1)

eli
eli

Reputation: 11

Do the SDL_Quit() before the IMG_Quit().

It seems IMG_Quit() has to be last function called.

Also looks like best practice to have a specific IMG_Init() somewhere.

Upvotes: 1

Related Questions