Crashes with SDL and libthr

From: Christian Gusenbauer <c47g_at_gmx.at>
Date: Sat, 24 Nov 2007 15:25:05 +0100
Hi!

I've just updated to today's current and now some SDL based applications are 
crashing. I debugged a bit and found out, that the changes made to libthr are 
causing these crashes. The attached program crashes immediately using the 
current libthr. This program does not crash when I'm using an old version of 
libthr (before 2007/11/21).

/*
compiled with:
cc -g -I/usr/local/include -L/usr/local/lib sdltest.c -lSDL -lthr -o sdltest
*/

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>

#define SAMPLE_FREQ     44100
#define SAMPLE_SIZE     (512*2)

void AudioCallbackSDL(void *udata, u_int8_t *buffer, int length)
{
}

main()
{
    SDL_AudioSpec audioSpec;
    SDL_AudioSpec mySpec;

    if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_AUDIO ) < 0) {
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }

    memset((void*)&audioSpec, 0, sizeof(SDL_AudioSpec));
    memset((void*)&mySpec, 0, sizeof(SDL_AudioSpec));

    audioSpec.freq = SAMPLE_FREQ;
    audioSpec.format = AUDIO_U8;
    audioSpec.channels = 1;
    audioSpec.samples = SAMPLE_SIZE/(44100/SAMPLE_FREQ);
    audioSpec.callback = AudioCallbackSDL;
    audioSpec.userdata = (void*)1;
    if (SDL_OpenAudio(&audioSpec, &mySpec) < 0) {
        fprintf(stderr, "can't open SDL audio: %s\n", SDL_GetError());
        exit(1);
    }

    sleep(10);
}

---------------------------------------------------------------------------

I debugged SDL and found the following piece of code, which causes the crash
in SDL_thread.c (around line 249).

        args->func = fn;
        args->data = data;
        args->info = thread;
        args->wait = SDL_CreateSemaphore(0);
                     ^
                     + here a semaphore is created with initial value 0

        if ( args->wait == NULL ) {
                SDL_free(thread);
                SDL_free(args);
                return(NULL);
        }

        /* Add the thread to the list of available threads */
        SDL_AddThread(thread);

        /* Create the thread and go! */
#ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
        ret = SDL_SYS_CreateThread(thread, args, pfnBeginThread, 
pfnEndThread);
#else
        ret = SDL_SYS_CreateThread(thread, args);
#endif
        if ( ret >= 0 ) {
                /* Wait for the thread function to use arguments */
                SDL_SemWait(args->wait);
                ^
                + here the calling thread should wait for the newly created
                  thread (but it doesn't)
        } else {
                /* Oops, failed.  Gotta free everything */
                SDL_DelThread(thread);
                SDL_free(thread);
                thread = NULL;
        }
        SDL_DestroySemaphore(args->wait);
        SDL_free(args);
        ^
        + here the args are freed, which causes the crash of the newly
          created thread. So I assume something's wrong with the semaphore
          operations

The newly created thread crashes, because he uses the args area passed as 
parameter to the SDL_SYS_CreateThread call and the parent thread is not 
waiting as it is supposed to.

Could someone please have a look at these libthr changes?

Many thanks,
Christian.

Received on Sat Nov 24 2007 - 13:50:59 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:23 UTC