SDL_DestroyRenderer() fails with `corrupted size vs. prev_size` (Maybe a bug)

I’m doing a simple code to learn how to make the main game loop more modular, but when closing the game it fails on SDL_DestroyRenderer() with errorr corrupted size vs. prev size and aborts.

The same do not happens when running the monolithic version of the same code.

Is this a bug in SDL or in my code?

Makefile (189 Bytes)
calls.c (3.8 KB)
mono.c (4.1 KB)

Can I make a suggestion to try making game->renderer a global variable on its own instead of part of the game struct?

Also, try printing the pointer address after SDL_CreateRenderer and again before SDL_DestroyRenderer to confirm that it hasn’t changed for some reason.
You can do this with
printf("%p\n", renderer);

I prefer to avoid globals in all cases. But yes. I can do it.

Anyway, after playing with valgrind I found the issue and isn’t on Renderer, but on Event.

On calls.c:96, it’s written:

game->event = malloc(sizeof (game->event));

This malloc is wrong. Replacing for:

game->event = malloc(sizeof (SDL_Event));

Fix the issue.

Ah! That did actually catch my eye, but I’m not a compiler so I didn’t know for sure.

game->event is a pointer to an SDL_Event structure. You can get the right size by dereferencing:-

game->event = malloc(sizeof (*game->event));

May look tidier. :wink:

1 Like

Doing calls like tthis printf("In main: &%p %p\n", (void*)&(game->renderer), (void*)(game->renderer));

DBG:                            Before create &0x5559368ee2f0 (nil)
DBG:                             After create &0x5559368ee2f0 0x555936ddc050
DBG:                                In  main  &0x5559368ee2f0 0x555936ddc050
DBG:                          Before destroy  &0x5559368ee2f0 0x555936ddc050
DBG:           In game_destroy before destroy &0x5559368ee2f0 0x555936ddc050
DBG:     In game_destroy right before destroy &0x5559368ee2f0 0x555936ddc050
DBG:      In game_destroy right after destroy &0x5559368ee2f0 0x555936ddc050
DBG:                           After destroy  &0x5559368ee2f0 (nil)