108

Every time I load the app it stops as if I had set a breakpoint on this line:

self.audioPlayer = 
 [[[AVAudioPlayer alloc] initWithData:[dataPersister loadData:self.fileName] 
                                error:&outError] autorelease];

There's no breakpoint above or any place near this line. It only happens when I run the app in debug mode and nothing crashes after the breakpoint. The app works as nothing happened when I click "Continue program execution".

This is the loadData method, which is called with initWithData:

-(NSData*)loadData:(NSString*)fileName
{
    NSString *dataPath = [self.path stringByAppendingPathComponent:fileName];
    dataPath = [dataPath stringByStandardizingPath];
    NSData *data = [[[NSData alloc] initWithContentsOfFile:dataPath]autorelease ];
    return data;
}

The loadData function seems to be working fine. The requested mp3 file is loaded and played without any problems after the breakpoint.

Do you have any idea what I'm doing wrong?

EDIT: I ran a backtrace when it stops at the breakpoint. This was the output:

(lldb) bt
* thread #1: tid = 0x1c03, 0x30df1724 libc++abi.dylib`__cxa_throw, stop reason = breakpoint 1.2
    frame #0: 0x30df1724 libc++abi.dylib`__cxa_throw
    frame #1: 0x36403a24 AudioToolbox`ID3ParserHandle::ID3ParserHandle(void*, long (*)(void*, unsigned long, unsigned long, unsigned long, void**, unsigned long*)) + 452
    frame #2: 0x36403b0e AudioToolbox`ID3ParserOpen + 142
    frame #3: 0x3635bd16 AudioToolbox`MPEGAudioFile::ParseID3Tags() + 58
    frame #4: 0x3635b9aa AudioToolbox`MPEGAudioFile::ParseAudioFile() + 26
    frame #5: 0x3631723e AudioToolbox`AudioFileObject::DoOpenWithCallbacks(void*, long (*)(void*, long long, unsigned long, void*, unsigned long*), long (*)(void*, long long, unsigned long, void const*, unsigned long*), long long (*)(void*), long (*)(void*, long long)) + 166
    frame #6: 0x36316480 AudioToolbox`AudioFileOpenWithCallbacks + 612
    frame #7: 0x31f4c1ec AVFoundation`-[AVAudioPlayer initWithData:error:] + 120

"SOLUTION": It turns out, if I disable exception breakpoint for all exceptions and only use breakpoint for Objective-C exceptions the problem disappears. But it doesn't solve the problem that the allocation of AVAudioPlayer throws a C++ exception.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
ThomasCle
  • 6,792
  • 7
  • 41
  • 81
  • 2
    I had the same stack trace. Disabling the "All Exceptions" breakpoint made it not happen for me. – makdad Apr 25 '12 at 07:55

6 Answers6

175

Add your exception breakpoint and edit the exception type from "All" to "Objective-C exceptions"

Some classes in AudioToolbox throw regular C++ exceptions. You can filter them off this way.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
Mugunth
  • 14,461
  • 15
  • 66
  • 94
  • 6
    As I have written the the "SOLUTION" section in my question, I do not consider this as a solution. The `AVAudioPlayer` shouldn't throw random exceptions. – ThomasCle Oct 22 '12 at 06:47
  • 10
    "AVAudioPlayer shouldn't throw random exceptions" - This is not in our hands. That's the way the framework is written and you have to live with it. Though, i agree that it's not a good idea – Mugunth Jan 11 '13 at 17:52
  • @Mugunth Does this mean that the framework is responding to and correcting internal errors? Are you aware of any other documentation on it? I can't find much online which suggests to me that this may be down to programmer error... – Remover Sep 13 '13 at 09:35
21

AVAudioPlayer and AVAudioRecorder both will throw exceptions, several of them. These are handled internally by the players but if you have a breakpoint for "All Breakpoints" (i.e. Exception: All, Break: On Throw) you will catch these exceptions. If you continue execution on these, the app will continue to run normally and not crash at all.

The only solution I've come up with so far is to click on the breakpoint bar in the Breakpoint Navigator, disabling this particular breakpoint, and running with it disabled.

When/if the app ever crashes with a thrown exception, I cmd-6, enable that breakpoint, and rerun and do whatever I did when it crashed.

Edit: setting to "Objective-C exceptions" is obviously how to do it. See above answer!

Kalle
  • 13,186
  • 7
  • 61
  • 76
9

Here's a screenshot showing how I fixed this error. I'm not sure if this is the same way that the answers above are talking about, but I assume its similar.

  1. Go to the Breakpoint navigator in Xcode.
  2. Control-click on the 'All Exceptions' line.
  3. Select the 'Edit Breakpoint...' option.
  4. Change the Exception from All to Objective-C.

enter image description here

Stewart Macdonald
  • 2,062
  • 24
  • 27
2

The backtrace helped a lot, thanks!. We'd started running into the same issue recently. It turns out the mp3 files it was throwing on did not have a valid ID3 tag and running them through an app such as Tagr fixed them right up!

yo.ian.g
  • 1,354
  • 2
  • 14
  • 21
  • 1
    After setting missing fields in the ID3 tag, the app did no longer stop at the initialization of the AVAudioPlayer, but it still stopped at the play instruction... – Reinhard Männer Oct 15 '14 at 06:46
  • Which fields were required? All of them? – reggian Nov 02 '16 at 14:33
  • @Reggian I don't recall, it was so long ago. Sorry about that! I don't remember needing to add any fields that were missing, just running it through a tag sanitizer was enough if I remember correctly! – yo.ian.g Nov 04 '16 at 18:25
2

In Xcode 9.2 you can disable specific exceptions after you've seen them. Open breakpoints menu and click to disable (faded arrow)

enter image description here

quantumpotato
  • 9,637
  • 14
  • 70
  • 146
-4

Try setting the AVAudioPlayer as a class Variable!

redestructa
  • 1,182
  • 1
  • 11
  • 11
  • 1
    In my case I already set up AVAudioPlayer as a class variable and it still doesn't work. Looks like you didn't really know the solution – Alex Cio Mar 31 '15 at 14:31