A simple program which creates 2 windows and a renderer for each of them is segfaulting some of the time. I am trying to understand if I am messing up something simple.
Here is code that reproduces the segfault most of the time, which I have tried on two different Linux machines (one Arch, one Debian 11).
#include "SDL.h"
int main(void) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window1 = SDL_CreateWindow("1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 250, 250, SDL_WINDOW_RESIZABLE);
SDL_Window *window2 = SDL_CreateWindow("2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1000, 1000, SDL_WINDOW_RESIZABLE);
SDL_Renderer *renderer1 = SDL_CreateRenderer(window1, -1, 0);
SDL_Renderer *renderer2 = SDL_CreateRenderer(window2, -1, 0);
SDL_Quit();
return 0;
}
I compile this with
gcc -Wall -Wpedantic -O3 -o bug bug.c `sdl2-config --cflags --libs`
Which only complains about unused variables.
Sample output of running it, when it segfaults:
[aps@archlinux ~]$ ./bug
Segmentation fault (core dumped)
I believe both windows are being created, as they do seem to pop up briefly before the segfault (though one covers the other so for all I know only one might be created).
When it does not segfault, there is as may be expected no output.
I think it is likely I am missing something obvious, but I have not been able to find out what the problem is by looking at the code. Any help would be appreciated.
Edit: I am sometimes unable to reproduce the segfault for long stretches until it happens again. There’s no detectable rhyme or reason.