Skip to content

Instantly share code, notes, and snippets.

@kommen
Created November 11, 2011 15:17
Show Gist options
  • Save kommen/1358237 to your computer and use it in GitHub Desktop.
Save kommen/1358237 to your computer and use it in GitHub Desktop.
EXC_BAD_ACCESS is _class_getName when throwing NSException with ARC enabled
Throwing an exception like this:
@throw [NSException exceptionWithName:@"Exception" reason:@"none" userInfo:nil];
in a method, and trying to catch it in a method up the stack, causes a EXC_BAD_ACCESS in:
#0 0x36221fd0 in _class_getName ()
#1 0x36221fbc in object_getClassName ()
#2 0x362261fe in objc_exception_throw ()
But only on an iOS device.
The exact same code works as expected without a crash on the simulator.
Workarounds:
* Use CFBridgingRetain() on the exception (shown in the example project)
* or store the exception to a local variable first and then throw it
Filed as rdar://10330762
@tylercollier
Copy link

I'm seeing this too. I can confirm the second workaround, but not really sure what the first means. I came across this from google. What example project are you referencing?

@kommen
Copy link
Author

kommen commented Jan 13, 2012

@tylercollier: I've uploaded the example project I sent with this rdar here: https://github.com/kommen/ARCThrowTest

@tylercollier
Copy link

@kommen, I tried your project on my phone and did NOT receive a EXC_BAD_ACCESS halt! I only have an iPhone and not an iPad so modified it quickly and provisioned it for my device. Unfortunately, the problem is not reproducable with your project on my iPhone. Not sure if that's good news or bad.

@kommen
Copy link
Author

kommen commented Jan 17, 2012

@tylercollier: what version of Xcode are you using, and on which version of iOS are you running on the device?

@tylercollier
Copy link

Xcode 4.2.1 build 4D502, iOS 5.0.1 (9A405). I found your post because I was having the same problem with my own app, but your app doesn't produce the same observations. I'm not sure what the difference is.

@kommen
Copy link
Author

kommen commented Jan 22, 2012

@tylercollier: It seems the specific bug I have reported to Apple and which the demo exposes was fixed, as it doesn't crash any more for me. It did definitely crash at the time of reporting, I confirmed that with various Xcode/iOS combos back then.

So it seems you're seeing a different, but maybe similar issue?

@tylercollier
Copy link

@kommen, might I have a fixed version of Xcode/iOS now too? My issue was exactly the same as yours. Using [NSException exceptionWithName] would cause EXC_BAD_ACCESS on a device but not the simulator. Using your project, which I modified slightly to run on an iPhone instead of an iPad because I have the former and not the latter, I don't see that issue. And I just reran my project and the EXC_BAD_ACCESS did not happen.

@kommen
Copy link
Author

kommen commented Jan 23, 2012

@tylercollier: Yeah, so it looks like Apple fixed that then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment