Avaudioplayer Throws Breakpoint in Debug Mode

AVAudioPlayer throws breakpoint in debug mode

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.

XCTAssertThrows stops at breakpoint

Unfortunately, it does not seem possible to have exception breakpoints ignore anything wrapped in XCTAssertThrows (or now XCTAssertThrowsError).

Workaround 1: Deal with the breakpoints or script around them

When you execute these tests, you can either do the following:

  • Disable the exception breakpoint.
  • Hit run every time it hits one of your breakpoints.

Both are not ideal as you'll be doing some manual work.

There are suggestions for how to disable breakpoints for Swift XCTAssertThrowsError assertions here. I have not tested those.

Workaround 2: Return Result<T, Error> instead

My original method looked like this:

struct Truck {
static func makeTruck(numberOfWheels: Int) throws -> Self {
if numberOfWheels < 4 {
throw TruckError.tooFewWheels
}

// ...

return .init()
}
}

// Code example:
let truck: Truck? = try? Truck.makeTruck(numberOfWheels: 1)

// Test example:
XCTAssertThrowsError(try Truck.makeTruck(numberOfWheels: 1))

To work around this problem of breakpoints, I created a duplicate internal function that is more testable (since it won't cause the breakpoint problem) by returning a Result instead of throwing an Error.

struct Truck {
static func _makeTruck(numberOfWheels: Int) -> Result<Truck, Error> {
if numberOfWheels < 4 {
return .failure(TruckError.tooFewWheels)
}

// ...

return .success(.init())
}

static func makeTruck(numberOfWheels: Int) throws -> Self {
switch _makeTruck(numberOfWheels: numberOfWheels) {
case .failure(let error):
throw error
case .success(let value):
return value
}
}
}

// Code example:
let truck: Truck? = try? Truck.makeTruck(numberOfWheels: 1)

// Test example:
XCTAssertEquals(Truck._makeTruck(numberOfWheels: 1), .error(TruckError.tooFewWheels))

Workaround 3: Return T? instead

Finally, if you have a simpler use-case, and your function's failure is quite obvious (e.g. there is only one type of error it can return), consider just returning an optional instead of needing the complexity of the Result type:

struct Truck {
static func makeTruck(numberOfWheels: Int) -> Truck? {
if numberOfWheels < 4 {
return nil
}

// ...

return .init()
}
}

// Code example:
let truck: Truck? = Truck.makeTruck(numberOfWheels: 1)

// Test example:
XCTAssertNil(Truck.makeTruck(numberOfWheels: 1))

Exception breakPoint at [self.window makeKeyAndVisible]

Edit Exception breakpoint like this-

Sample Image

And select Objective C like this-

Sample Image

Hope this helps!

Why does AudioServicesCreateSystemSoundID throw an exception internally but return 0 as an error code?

C++ libraries may throw and catch exceptions internally for all sorts of reasons, for example end of buffer or end of file. Whether this is an appropriate use of exceptions, good coding style or software engineering practice is debatable. As long as an exception does not make it uncaught into your code you should not worry about it.

You say that the routine returns successfully and the desired output is obtained, so nothing is wrong (i.e. it is not a bug).

Always stop in App delegate after enabling All exceptions break point

Only enable Objective-C breakpoints.

To see the actual statement that is causing the error add an exception breakpoint:

  1. From the Main Menu Debug:Breakpoints:Create Exception Breakpoint.

  2. Right-click the breakpoint and set the exception to Objective-C. This will ignore other types of exceptions such as from C++. There are parts of the APIs that use exceptions such as Core Data (Apple is Special).

  3. Add an action: "po $arg1".

Run the app to get the breakpoint and you will be at the line that causes the exception and the error message will be in the debugger console.

Breakpoint example:

Sample Image

Breakpoint stuck at main function

Maybe You've added an Exception break point..., Add an Objective-C breakpoint like this :-

Sample Image

AVAudioRecorder throwing exception on prepareToRecord

I'm getting this exception, too. Searching for solution. Important to note that you can only see these exceptions if you enable exception breakpoints. The app perform properly, but this is concerning. I'll keep investigating.

EDIT:
well, the answer is here: AVAudioPlayer throws breakpoint in debug mode
i figured that much myself, but still feel uncomfortable about having these exceptions. using different audio format while recording makes the exceptions to go away. however, i want to use the lossless format, which does cause these exceptions. hope this helps.



Related Topics



Leave a reply



Submit