Sound Error Crashes Corona Simulator on Mac, app on iOS, but never crashes on Windows or Android

Totally baffled here.

As the subject states, this is only something that started happening when I introduced music to my game.

I’m currently running the latest daily build, and about every 5-10 minutes the corona simulator will crash on the Mac and iOS. iOS and OSX versions behave exactly the same, they crash every 5-10 minutes.

This is a problem that I cannot replicate on Windows Corona Simulator or on Android. The game will run literally for hours without a single crash.

This is the error that terminal spits out in OSX:

Corona Simulator(12137,0xac8242c0) malloc: *** error for object 0x5a8464: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
/Applications/CoronaSDK/Corona Terminal: line 9: 12137 Abort trap: 6 “$path/Corona Simulator.app/Contents/MacOS/Corona Simulator” $*
logout

I use director.lua, and what I’m trying to do is have music play during the main menu and shop screens and such. Initially I used mp3’s for everything and thought that perhaps OSX and iOS don’t like MP3s as much as AAC, so I converted it to AAC. That didn’t do anything. Then I thought that maybe OSX and iOS don’t like audio.loadStream, so I changed the main background music file to audio.loadSound. But that didn’t do anything either.

In my main.lua I do this:

–backgroundMusic = audio.loadStream( “menumusic.mp3”)
–backgroundMusic = audio.loadStream( “menumusic.m4a”)
backgroundMusic = audio.loadSound( “menumusic.m4a”)

and since I’m using director, this is now a global variable that is accessible to all classes. When the game goes to start playing I do the following:

[code]
audio.fade({ channel=1, time=200, volume = 0 } )
local function pauseMusic()
audio.pause(backgroundMusic)
end

timer.performWithDelay(210,pauseMusic,1)
timer.performWithDelay ( 350, director:changeScene( “loadplaygame” ),1)[/code]

This is because I don’t want the music playing while the game is active, but I do want the music to continue from where it left off when the player returns to the menu or shop. So once the game is over, I fade the music back in and resume it. This is why I don’t free it up, and why I don’t use audio.fadeOut.

Unfortunately, I cannot for my life figure out what’s causing the crash. I have a feeling the garbage collector on OSX and iOS is trying to free up the memory, and it’s crashing the simulator/app, but I never ever evoke audio.stop, or play any sounds on the same channel. I’ve tried a million different things, but the behavior is always exactly the same, and it baffles me why it is only on OSX and iOS and doesn’t affect Android or Windows.

I listed it here because I believe it’s a Corona bug. (and will be embarrassed if it’s my own fault) [import]uid: 37122 topic_id: 26514 reply_id: 326514[/import]

Additionally, this is an alternate error that appears in terminal when Corona crashes:

/Applications/CoronaSDK/Corona Terminal: line 9: 12287 Bus error: 10 “$path/Corona Simulator.app/Contents/MacOS/Corona Simulator” $*
logout
[import]uid: 37122 topic_id: 26514 reply_id: 107506[/import]

The type of error you see is usually caused by calling audio.dispose() on the handle and then trying to reuse it later. (Or calling audio.dispose on a playing sound may also cause similar problems.) I would make sure your code that your code (including Director) is not calling audio.dispose.

Another possibility is that somebody is using the global variable ‘backgroundMusic’ somewhere else and the variable is getting changed out from under you. Passing a bad handle to the audio APIs may cause crashes like this too.
Since you switched to audio.loadSound (which was a really good thought) and you still get the problem, I suspect the problem is an error in your code somewhere and you are just getting *lucky* that Android/Windows is not crashing. audio.loadSound is pretty dead simple behind the scenes (unlike loadStream) so I consider it unlikely this is a Corona bug since we haven’t really changed code that would affect this on iOS/Mac in a really long time and nobody else is having this problem.
If you think you found a real bug, please file a bug report with us with a simple reproducible test case (i.e. remove director). We don’t always see these forum posts; consider yourself lucky I saw this thread.
[import]uid: 7563 topic_id: 26514 reply_id: 107520[/import]

@ewing I have a question regarding audio.dispose() when you say “The type of error you see is usually caused by calling audio.dispose() on the handle and then trying to reuse it later.”

Let’s say in my main menu you have:
[lua]local soundtrack = audio.loadStream(“soundtrack02.m4a”)
local backgroundMusicChannel = audio.play(soundtrack, {channel=1, loops=-1})

–and then later before changing scenes to game.lua with Director I do

audio.stop()
audio.dispose(soundtrack)
soundtrack = nil;
backgroundMusicChanel = nil;[/lua]

Will this give me an error when the player comes back to the the menu.lua file? since “soundtrack” will be created again and is being used “later”?

Or do I need to check if soundtrack exists before loading up another audio.loadStream?

Because right now I’m getting some crazy errors every time I switch scenes with director and I feel like it might be audio related.

"/Applications/CoronaSDK/Corona Terminal: line 9: 3630 Bus error: 10 “$path/Corona Simulator.app/Contents/MacOS/Corona Simulator” $*
logout
" [import]uid: 123298 topic_id: 26514 reply_id: 109421[/import]

I don’t know anything about Director or the code flow. I can’t tell from that alone if the code is safe.

audio.loadStream will load multiple instances of an audio file if you tell it to. This is by design. If you don’t want it loaded multiple times, then you should check if it is loaded.

Similarly, you need to be careful about which instance you are unloading if you do have multiple instances. Otherwise, you may say create instance 1, then create instance 2 and reassign it to the first variable. Then when you dispose, you dispose the second one twice in a row which causes a problem (and leak the first).

[import]uid: 7563 topic_id: 26514 reply_id: 109427[/import]

Thanks for the info ewing.

I removed all the audio from my game and I’m still getting the same errors.

The only thing that changed before adding audio was replacing all my images with the new imageSheet API. So I guess I need to double check how I’m handling that when scene changes happen. [import]uid: 123298 topic_id: 26514 reply_id: 109428[/import]