Segmentation fault with multiple windows

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.

Note: I had to render something to make the windows appear because I’m on wayland.

I don’t know what’s exactly going wrong but since it seemed to segfault at SDL_CreateRenderer I created the renderer for the window right after it was created and it worked fine.

	SDL_Window *window1 = SDL_CreateWindow("1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 250, 250, SDL_WINDOW_RESIZABLE);
	SDL_Renderer *renderer1 = SDL_CreateRenderer(window1, -1, 0);

	SDL_Window *window2 = SDL_CreateWindow("2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1000, 1000, SDL_WINDOW_RESIZABLE);
	SDL_Renderer *renderer2 = SDL_CreateRenderer(window2, -1, 0);

Though the segfaults are kind of weird since they didn’t occur when I was running 2 instances of it where one of them segfaulted in gdb while the other seemed to run fine. So I thought of running another SDL2 program (X11 programs seems to accomplish the same) and running the program and the segfaults didn’t happen. It seemed to be running just fine.

Edit: This seems to be a X11 specific problem and doesn’t happen in wayland, specifically X11_XUndefineCursor in src/video/x11/SDL_x11mouse.c:304.

I added this and it seemed to be working fine. I don’t know if it’s appropriate since I just guessed the number.

if (x11_cursor > 0) {
    X11_XUndefineCursor(display, data->xwindow);
}
1 Like

Interesting. So it’s a problem in the library code? Should I make an issue on GitHub?

So it’s a problem in the library code?

Most likely, especially because it works fine in wayland.

Should I make an issue on GitHub?

Yes. I’m not sure if it’s an issue with X11 or SDL but either way SDL should probably handle it somehow instead of segfaulting.

Alright; thank you for the help. I made an issue.

Is there a workaround for the bug? I cant get a new version so i need one.