We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2760

Summary: Seg fault when creating multiple windows.
Product: SDL Reporter: JD <jdowdells>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED WONTFIX QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: icculus
Version: 2.0.3Keywords: triage-2.0.4
Hardware: x86_64   
OS: Linux   

Description JD 2014-10-20 14:14:45 UTC
I create a couple of windows to display YUV data from 2 separate pthreads.  When the 2nd window is created I get a seg fault in the call to  "SDL_PollEvent (&event)" the seg fault happens 2 places:

SDL_x11events.c: 

...
    if (_this->suspend_screensaver) {
        Uint32 now = SDL_GetTicks();
        if (!data->screensaver_activity

At this point, "data" is NULL.

and

SDL_x11mouse.c:

            data = (SDL_WindowData *)window->driverdata;

            if (x11_cursor != None) {
    if( data->xwindow )
                X11_XDefineCursor(display, data->xwindow, x11_cursor );
            } else {
                X11_XUndefineCursor(display, data->xwindow);
            }

again, driverdata is NULL.

It looks to me like the PumpEvents funciton is being called too early when the 2nd window is created.  The first window has been set up properly but the 2nd window has NULL in the driverdata field.

I can work round it by catching the NULL case and ignoring the call.

Here is the code I used to create the windows:

void DisplayTask (void *arg)
{
	unsigned int ctxId = *(unsigned int *)arg;

    SDL_Surface *fontSurface[2];
    SDL_Texture *fontTex[2];
    SDL_Surface *alteraSurface[2] = { NULL, NULL };
    SDL_Texture *alteraTex[2] = { NULL, NULL };

	printf("DisplayTask_SDL: started, context id = %d\n", ctxId );
    

    static SDL_Window *win[MAX_CONTEXT];
    static SDL_Renderer *ren[MAX_CONTEXT];
    static SDL_Texture *tex[MAX_CONTEXT] = { NULL, NULL };

    SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };


        win[ctxId] = SDL_CreateWindow ("Altera HEVC Demo", 0, 0, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);


    if (win[ctxId] == NULL)
    {

        printf ("Can't create window\n");
        SDL_Quit ();

        exit (-1);
    }

    ren[ctxId] = SDL_CreateRenderer (win[ctxId], -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    if (ren[ctxId] == NULL)
    {
        printf ("Can't create renderer\n");
        SDL_DestroyWindow (win[ctxId]);
        SDL_Quit ();
        exit (-1);
    }

    tex[ctxId] = SDL_CreateTexture (ren[ctxId], SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);

    if (!tex[ctxId])
    {
        printf ("Tex create failed ror %p\n", ren[ctxId]);
        exit (-2);
    }

...
}


I have a separate thread that is doing this:


void *SDL_InputThread( void *arg )
{
	static SDL_Event event;

        sem_wait (&inputSem );

	SDL_ShowCursor (0);

	memset( &event, 0, sizeof( SDL_Event ));

	while(bRunning) 
	{
		if (SDL_PollEvent (&event))	// only do this for 1 context
		{
		    switch (event.type)
		    {
Comment 1 Ryan C. Gordon 2015-02-19 06:32:15 UTC
Marking a large number of bugs with the "triage-2.0.4" keyword at once. Sorry
if you got a lot of email from this. This is to help me sort through some bugs
in regards to a 2.0.4 release. We may or may not fix this bug for 2.0.4,
though!
Comment 2 Ryan C. Gordon 2015-04-07 04:22:50 UTC
SDL generally assumes all your GUI stuff happens from one thread (as do many platforms), so there are likely places like this that aren't thread-safe. You might get away with it if you use a semaphore (etc) to wait until all the windows are created to pump the event queue, but this is a risk in any case.

--ryan.