Audio initialization causes crash on OS X 10.4.11

I’m in the process of porting my SDL game to Mac OS X. I’ve been able to get
it running on my own Mac without any problems, but a user is reporting that
the game always crashes right on startup. I’ve been able to locate the crash
to this:

IF SDL_INIT(SDL_INIT_AUDIO) < 0 THEN BEGIN
WriteToLog(‘Error: Unable to initialize SDL audio!’);
SDL_Quit;
Exit;
END;

If I take out the audio initializaion, it works perfectly (until the game
tries to play music, of course). Here’s the trace back from the user:

Date/Time: 2008-09-15 18:29:00.571 -0600
OS Version: 10.4.11 (Build 8S165)
Report Version: 4

Command: asciisec
Path: /Volumes/Bob/asciisector/Privateer - Ascii Sector/Privateer - Ascii
Sector.app/Contents/MacOS/asciisec
Parent: WindowServer [101]

Version: ??? (???)

PID: 14890
Thread: 0

Exception: EXC_ARITHMETIC (0x0003)
Code[0]: 0x00000003
Code[1]: 0xc6002090

Thread 0 Crashed:
0 …audio.toolbox.AudioToolbox 0x9421c3c4
SincKernel::CalculateFilterCoefficients(double) + 596
1 …audio.toolbox.AudioToolbox 0x9428c018
SincKernelFactory::ReferenceSincKernel(double, int, int) + 152
2 …audio.toolbox.AudioToolbox 0x9428bed0 Resampler2::Resampler2[unified]
(double, double, int) + 628
3 …audio.toolbox.AudioToolbox 0x9421bfc0
Resampler2Wrapper::Resampler2Wrapper[unified](SampleRateConverter*, int,
double, double, int) + 168
4 …audio.toolbox.AudioToolbox 0x9421bea4
SampleRateConverter::SetConverterQuality(unsigned long) + 104
5 …audio.toolbox.AudioToolbox 0x9421be10
SampleRateConverter::SampleRateConverter[unified](StreamDescPair const&) + 244
6 …audio.toolbox.AudioToolbox 0x94218be8
PCMConverterFactory::BuildConverterChain(StreamDescPair const&,
AudioConverterChain*, PCMConverterFactory*) + 704
7 …audio.toolbox.AudioToolbox 0x94217df8 ConverterRegistry::CreateConverter
(StreamDescPair const&, AudioConverterChain**, unsigned long,
AudioClassDescription*) + 104
8 …audio.toolbox.AudioToolbox 0x94217754 AudioConverterNew + 356
9 …pple.audio.units.Components 0x700c96b8 0x70000000 + 825016
10 …pple.audio.units.Components 0x700c9938 0x70000000 + 825656
11 …pple.audio.units.Components 0x7000778c DefaultOutputAUEntry + 23004
12 …pple.audio.units.Components 0x70006f48 DefaultOutputAUEntry + 20888
13 …pple.audio.units.Components 0x70004934 DefaultOutputAUEntry + 11140
14 …pple.audio.units.Components 0x70006efc DefaultOutputAUEntry + 20812
15 …pple.audio.units.Components 0x70006e8c DefaultOutputAUEntry + 20700
16 …pple.audio.units.Components 0x70005018 DefaultOutputAUEntry + 12904
17 …pple.audio.units.Components 0x70004e94 DefaultOutputAUEntry + 12516
18 …pple.audio.units.Components 0x700d1d9c 0x70000000 + 859548
19 …ple.CoreServices.CarbonCore 0x90bdcb88 CallComponent + 260
20 …apple.audio.units.AudioUnit 0x9432b5fc AudioUnitInitialize + 32
21 SDL 0x300380ec SDL_HasRDTSC + 3924
22 SDL 0x300023a4 SDL_OpenAudio + 864
23 SDL_mixer 0x32017f64 Mix_OpenAudio + 116
24 …hristianknudsen.asciisector 0x00038710 TOOLBOX_PROGRAMSTART + 1840
(toolbox.pas:495)
25 …hristianknudsen.asciisector 0x0021c448 P$ASCIISEC_STARTGAME + 56
(asciisec.pas:411)
26 …hristianknudsen.asciisector 0x0021c9a4 PASCALMAIN + 52
(asciisec.pas:508)
27 …hristianknudsen.asciisector 0x00282c48
SYSTEM_FPC_SYSTEMMAIN$LONGINT$PPCHAR$PPCHAR + 40
28 …hristianknudsen.asciisector 0x00003630 -[SDLMain
applicationDidFinishLaunching:] + 76
29 com.apple.Foundation 0x92bf1a5c _nsnote_callback + 180
30 com.apple.CoreFoundation 0x90805ec0 __CFXNotificationPost + 368
31 com.apple.CoreFoundation 0x907fdf20 _CFXNotificationPostNotification + 684
32 com.apple.Foundation 0x92bdbe64 -[NSNotificationCenter
postNotificationName:object:userInfo:] + 92
33 com.apple.AppKit 0x937b2be8 -[NSApplication _postDidFinishNotification] +
112
34 com.apple.AppKit 0x937b2ad4 -[NSApplication
_sendFinishLaunchingNotification] + 92
35 com.apple.AppKit 0x937b261c -[NSApplication(NSAppleEventHandling)
_handleAEOpen:] + 264
36 com.apple.AppKit 0x937b21c4 -[NSApplication(NSAppleEventHandling)
_handleCoreEvent:withReplyEvent:] + 92
37 com.apple.Foundation 0x92bf2a68 -[NSAppleEventManager
dispatchRawAppleEvent:withRawReply:handlerRefCon:] + 380
38 com.apple.Foundation 0x92bf28c8 _NSAppleEventManagerGenericHandler + 92
39 com.apple.AE 0x914fe960 aeDispatchAppleEvent(AEDesc const*, AEDesc*,
unsigned long, unsigned char*) + 208
40 com.apple.AE 0x914fe7fc dispatchEventAndSendReply(AEDesc const*, AEDesc*) +
44
41 com.apple.AE 0x914fe654 aeProcessAppleEvent + 284
42 com.apple.HIToolbox 0x932ad2e0 AEProcessAppleEvent + 60
43 com.apple.AppKit 0x937b090c _DPSNextEvent + 856
44 com.apple.AppKit 0x937b03f8 -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:] + 116
45 com.apple.AppKit 0x937ac93c -[NSApplication run] + 472
46 …hristianknudsen.asciisector 0x00003c7c main + 1316
47 …hristianknudsen.asciisector 0x000024ec _start + 760
48 …hristianknudsen.asciisector 0x000021f0 start + 48

Any ideas on what is causing this problem?

Nobody have an idea as to what’s causing the crash? It’s really annoying, as
it’s holding me back from releasing the Mac OS X version of my game. :frowning:

I’ve been able to locate the crash to this:

IF SDL_INIT(SDL_INIT_AUDIO) < 0 THEN BEGIN
WriteToLog(‘Error: Unable to initialize SDL audio!’);
SDL_Quit;
Exit;
END;

Thread 0 Crashed:
[…]
22 SDL 0x300023a4 SDL_OpenAudio + 864
23 SDL_mixer 0x32017f64 Mix_OpenAudio + 116
24 …hristianknudsen.asciisector 0x00038710 TOOLBOX_PROGRAMSTART + 1840

It looks like you’re actually not crashing where you think you are, at
least not directly.

Are you initializing SDL_mixer from another thread? Here’s what I
think is happening:

  • You’re calling SDL_Init(SDL_INIT_AUDIO) from one thread.
  • You’re calling Mix_OpenAudio() from another thread.
  • Mix_OpenAudio() calls SDL_OpenAudio(), which helpfully calls
    SDL_Init(SDL_INIT_AUDIO) for you if you haven’t called it yet.
  • Now you have two threads initializing the audio subsystem at the same
    time, and they step on each other…this machine is just unlucky enough
    to have a fast enough (or slow enough) set of CPU cores to not finish
    SDL_Init() before Mix_OpenAudio() calls it, too.

This is just a guess, though.

–ryan.

Ryan C. Gordon <icculus icculus.org> writes:

It looks like you’re actually not crashing where you think you are, at
least not directly.

Are you initializing SDL_mixer from another thread? Here’s what I
think is happening:

  • You’re calling SDL_Init(SDL_INIT_AUDIO) from one thread.
  • You’re calling Mix_OpenAudio() from another thread.
  • Mix_OpenAudio() calls SDL_OpenAudio(), which helpfully calls
    SDL_Init(SDL_INIT_AUDIO) for you if you haven’t called it yet.
  • Now you have two threads initializing the audio subsystem at the same
    time, and they step on each other…this machine is just unlucky enough
    to have a fast enough (or slow enough) set of CPU cores to not finish
    SDL_Init() before Mix_OpenAudio() calls it, too.

This is just a guess, though.

–ryan.

I’m not using threads, so that can’t be it. I localized the crash to this line
by systematically commenting out lines until the program crashed. When I comment
out the SDL_Init(SDL_INIT_AUDIO) – which I’ve later changed to
SDL_InitSubsystem, but that didn’t change anything – the program launches and
runs perfectly (without sound and music, of course).

I’m not using threads, so that can’t be it. I localized the crash to this line
by systematically commenting out lines until the program crashed. When I comment
out the SDL_Init(SDL_INIT_AUDIO) – which I’ve later changed to
SDL_InitSubsystem, but that didn’t change anything – the program launches and
runs perfectly (without sound and music, of course).

Can I see the source code? It would be easier to track down.

–ryan.

Ryan C. Gordon <icculus icculus.org> writes:

Can I see the source code? It would be easier to track down.

–ryan.

This is the procedure which loads up everything when starting the game:

PROCEDURE ProgramStart;
VAR InFile : Text;
TempString : String;
ErrorCode : Integer;
BEGIN
IF FileExists(‘data/options.txt’) THEN BEGIN
Assign(InFile, ‘data/options.txt’);
{$I-}
Reset(InFile);
ErrorCode := IOResult;
{$I+}
IF ErrorCode = 0 THEN BEGIN
Readln(InFile, TempString);
Val(TempString, SoundEffectsSetting, ErrorCode);
Readln(InFile, TempString);
Val(TempString, MusicSetting, ErrorCode);
Readln(InFile, TempString);
Val(TempString, GameSpeed, ErrorCode);
Readln(InFile, TempString);
Val(TempString, AutopilotSpeed, ErrorCode);
Readln(InFile, TempString);
Val(TempString, DialogueSpeed, ErrorCode);
Readln(InFile, TempString);
Val(TempString, BorderMarginX, ErrorCode);
Readln(InFile, TempString);
Val(TempString, BorderMarginY, ErrorCode);
Readln(InFile, TempString);
SlowComputer := CharBool(TempString);
CheckForFriendlyFire := T;
IF NOT EOF(InFile) THEN BEGIN
Readln(InFile, TempString);
CheckForFriendlyFire := CharBool(TempString);
END;
FullScreen := T;
IF NOT EOF(InFile) THEN BEGIN
Readln(InFile, TempString);
FullScreen := CharBool(TempString);
END;
DoubleSize := F;
IF NOT EOF(InFile) THEN BEGIN
Readln(InFile, TempString);
DoubleSize := CharBool(TempString);
END;
Close(InFile);
END
ELSE ShowIOError(ErrorCode, ‘data/options.txt’);
END
ELSE BEGIN
SoundEffectsSetting := 6;
MusicSetting := 3;
GameSpeed := 6;
AutopilotSpeed := 5;
DialogueSpeed := 5;
BorderMarginX := 40;
BorderMarginY := 24;
SlowComputer := False;
CheckForFriendlyFire := True;
FullScreen := False;
DoubleSize := F;
END;
{ Check that the ‘QUESTS’ folder exists }
{$I-}
ChDir(‘quests’);
IF IOResult <> 0
THEN MkDir(‘quests’)
ELSE ChDir(’…’);
{$I+}

Assign(InFile, ‘log.txt’);
{$I-}
Rewrite(InFile);
ErrorCode := IOResult;
{$I+}
IF ErrorCode = 0
THEN Close(InFile)
ELSE ShowIOError(ErrorCode, ‘log.txt’);

WriteToLog(‘Initializing SDL video…’);
IF SDL_INIT(SDL_INIT_VIDEO) < 0 THEN BEGIN
WriteToLog(‘Error: Unable to initialize SDL video!’);
SDL_Quit;
Exit;
END;
WriteToLog(‘Initializing SDL timer…’);
IF SDL_INITSUBSYSTEM(SDL_INIT_TIMER) < 0 THEN BEGIN
WriteToLog(‘Error: Unable to initialize SDL timer!’);
SDL_Quit;
Exit;
END;
WriteToLog(‘Initializing SDL audio…’);
IF SDL_INITSUBSYSTEM(SDL_INIT_AUDIO) < 0 THEN BEGIN
WriteToLog(‘Error: Unable to initialize SDL audio!’);
SDL_Quit;
Exit;
END;

Mix_OpenAudio(11100, MIX_DEFAULT_FORMAT, 2, 256);
Mix_AllocateChannels(16);
Mix_ReserveChannels(2);

Mix_VolumeMusic(Trunc((MusicSetting / 10) * 128));
WriteToLog(‘Loading sounds…’);
LoadSamples;
TempImage := SDL_LoadBMP(‘icon.bmp’);
SDL_WM_SetIcon(TempImage, 0);

IF DoubleSize THEN BEGIN
IF FullScreen
THEN TheScreen := SDL_SetVideoMode(1280, 800, 8, SDL_SWSURFACE OR
SDL_FULLSCREEN OR SDL_ANYFORMAT)
ELSE TheScreen := SDL_SetVideoMode(1280, 800, 8, SDL_SWSURFACE OR
SDL_ANYFORMAT);
END
ELSE BEGIN
IF FullScreen
THEN TheScreen := SDL_SetVideoMode(640, 400, 8, SDL_SWSURFACE OR
SDL_FULLSCREEN OR SDL_ANYFORMAT)
ELSE TheScreen := SDL_SetVideoMode(640, 400, 8, SDL_SWSURFACE OR
SDL_ANYFORMAT);
END;

WriteToLog(‘Setting video mode…’);
if (TheScreen = nil ) then begin
WriteToLog(‘Error: Unable to set video mode!’);
SDL_Quit;
Exit;
END;
SDL_ShowCursor(SDL_DISABLE);
SDL_WM_SetCaption(‘Privateer: Ascii Sector v0.4.5’, ‘Privateer: Ascii
Sector v0.4.5’);

LoadScreen;

Randomize;
SetAllKeysUp;
END;

Mix_OpenAudio(11100, MIX_DEFAULT_FORMAT, 2, 256);

Humor me and change that 11100 to 11025. It’s likely the game is
crashing because it’s trying to resample from a non-standard frequency,
which is causing a buffer overrun.

–ryan.

Ryan C. Gordon <icculus icculus.org> writes:

Mix_OpenAudio(11100, MIX_DEFAULT_FORMAT, 2, 256);

Humor me and change that 11100 to 11025. It’s likely the game is
crashing because it’s trying to resample from a non-standard frequency,
which is causing a buffer overrun.

–ryan.

I’ll give it a shot, but I’ve already tried systematically taking out the lines
related to audio (from first to last). Only when I removed the actual SDL
initialization of audio did it not crash.

I’ll give it a shot, but I’ve already tried systematically taking out the lines
related to audio (from first to last). Only when I removed the actual SDL
initialization of audio did it not crash.

If it doesn’t fix it, the next thing I’ll need is to run it under a
debugger; this is just psychic debugging right now. :slight_smile:

–ryan.

Ryan C. Gordon <icculus icculus.org> writes:

If it doesn’t fix it, the next thing I’ll need is to run it under a
debugger; this is just psychic debugging right now. :slight_smile:

–ryan.

Yeah, hopefully that would reveal the error. Anyway, I’ve changed the frequency
from 11100 to 11025 and sent the new executable to the guy that kept getting the
error. I’m just waiting to hear back from him now.

It’s kinda annoying that I’m not getting this error myself, as that would of
course make it easier to locate…

Yeah, hopefully that would reveal the error. Anyway, I’ve changed the frequency
from 11100 to 11025 and sent the new executable to the guy that kept getting the
error. I’m just waiting to hear back from him now.

Any reply yet? I’m curious if this helped.

–ryan.

Ryan C. Gordon <icculus icculus.org> writes:

Any reply yet? I’m curious if this helped.

–ryan.

Not yet. The guy who gets the error is just a guest poster at my forum, so I’m
not able to contact him. I think I’ll just release the OS X version of my game
anyway (with the 11025Hz fix) to see if anybody else is getting the crash. If
not, I’ll consider it fixed, but if other people get the crash, more crash
reports will likely help figure out what’s wrong.

Ryan C. Gordon <icculus icculus.org> writes:

Any reply yet? I’m curious if this helped.

–ryan.

I’ve heard back from another user. He’s on Mac OS X 10.5.5 and getting the same
crash (at least the game doesn’t start – I haven’t seen a trace back, yet, but
I’m assuming it’s the same thing with SDL audio initialization). This is with
the version that has audio initialization at 11025Hz.

The Mac OS X version can be downloaded at www.asciisector.net/download

Hi Christian,

I was excited by the screenshots of AsciiSector and downloaded the mac version.
Unfortunately here is what I get (GDB output):

"
Reading symbols for shared libraries +++++… done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries … done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done

              Program received signal EXC_ARITHMETIC, Arithmetic exception.
                                                                           0x9275de9c in SincKernel::CalculateFilterCoefficients ()
                                                   (gdb) 

"

Regards …

Julien

— En date de?: Mer 29.10.08, Christian Knudsen a ?crit?:> De: Christian Knudsen

Objet: Re: [SDL] Audio initialization causes crash on OS X 10.4.11
?: sdl at libsdl.org
Date: Mercredi 29 Octobre 2008, 0h16
Ryan C. Gordon <icculus icculus.org>
writes:

Any reply yet? I’m curious if this helped.

–ryan.

I’ve heard back from another user. He’s on Mac OS X
10.5.5 and getting the same
crash (at least the game doesn’t start – I haven’t
seen a trace back, yet, but
I’m assuming it’s the same thing with SDL audio
initialization). This is with
the version that has audio initialization at 11025Hz.

The Mac OS X version can be downloaded at
www.asciisector.net/download


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi Julien,

I think it’s fixed now. Could you please try redownloading the mac version?

I think the error was the one discussed here:
http://www.mail-archive.com/fpc-pascal at lists.freepascal.org/msg14046.html

Hi,

The game launches, but crashes rapidly. I should send you the gdb outputs in another place than here to avoid polluting the list.

Let me know

Julien

— En date de?: Mer 29.10.08, Christian Knudsen a ?crit?:> De: Christian Knudsen

Objet: Re: [SDL] Audio initialization causes crash on OS X 10.4.11
?: sdl at libsdl.org
Date: Mercredi 29 Octobre 2008, 19h15
Hi Julien,

I think it’s fixed now. Could you please try
redownloading the mac version?

I think the error was the one discussed here:
http://www.mail-archive.com/fpc-pascal at lists.freepascal.org/msg14046.html


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Please send them to @Christian_Knudsen. When does the game crash now?

julien CLEMENT <clementj2005 yahoo.fr> writes:

Hi,

The game launches, but crashes rapidly. I should send you the gdb outputs in
another place than here to avoid> polluting the list.

Let me know

Julien

Does the game crash/freeze when starting a new game, right after the commodity
market and missions have been set up? If this is the case, then I believe that’s
due to little-endian vs. big-endian when reading from the game’s data files.
This should be fixed now, so please redownload the mac version (again). Hope it
works now!