[SDL] [Need help] Why this code crached?

Mike Shal marfey at gmail.com
Sat Jan 20 13:07:09 PST 2007


On 1/20/07, David Olofson <david at olofson.net> wrote:
> On Saturday 20 January 2007 21:07, Igor Mironchick wrote:
> [...]
> >     static void audio_callback( void *userdata, Uint8 *stream,
> >                                       int [...] len );
> [...]
>
> You can't pass this function as a C callback! It needs
> an "invisible" 'this' argument to work.
>
> You need to use a C calling convension wrapper callback, that
> casts 'userdata' to the type of your class, and then forwards the
> call to your audio_callback().

I don't think this is the problem - he's using a static member
function as the callback, which doesn't have a 'this' pointer. And
he's only using static data in audio_callback(), so he doesn't need to
do the typical casting of userdata that you would expect.

When I ran it I had several issues:
1) main() was in the namespace - I assume this is a copy/paste error
2) SDL_OpenAudio failed - I had to set m_desired_spec.channels before
it would work for me (since this doesn't stop the program, it later
crashed in SDL_BuildAudioCVT())
3) The check to see if SDL_BuildAudioCVT() doesn't work (at least in
SDL1.2.7 - maybe this changed). The man page says it returns 1 on
success and -1 on error, but it's checking for != 0 to determine an
error condition.
4) And I believe this is the main issue: In the audio callback, you
call SDL_MixAudio with the amount of data left in your wave file. This
doesn't necessarily fit within the mix buffer (defined by the 'len'
argument in the callback). I think you want to pass in the min of
'waveleft' and 'len' to SDL_MixAudio.

With those changes I was able to get some sound out.
-Mike




More information about the SDL mailing list