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 ?
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 ?
Share Improve this question edited Feb 17 at 15:37 genpfault 52.1k12 gold badges91 silver badges148 bronze badges asked Feb 17 at 14:54 luke simluke sim 112 bronze badges New contributor luke sim is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 0Do the SDL_Quit() before the IMG_Quit()
It seems IMG_Quit() has to be last function called: https://wiki.libsdl./SDL2_image/IMG_Quit
Also looks like best practice to have a specific IMG_Init() somewhere