• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Debugging: Or How I Learned To Stop Worrying and Love EXC_BAD_ACCESS
 

Debugging: Or How I Learned To Stop Worrying and Love EXC_BAD_ACCESS

on

  • 2,609 views

Slides from my presentation at the March Tucson Cocoaheads meeting.

Slides from my presentation at the March Tucson Cocoaheads meeting.

Statistics

Views

Total Views
2,609
Views on SlideShare
2,571
Embed Views
38

Actions

Likes
1
Downloads
19
Comments
0

4 Embeds 38

http://tekhne.co 23
http://theupstart.co 11
http://feeds.feedburner.com 3
http://a0.twimg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Debugging: Or How I Learned To Stop Worrying and Love EXC_BAD_ACCESS Debugging: Or How I Learned To Stop Worrying and Love EXC_BAD_ACCESS Presentation Transcript

  • DEBUGGINGOR HOW I LEARNED TO STOP WORRYING AND LOVE EXC_BAD_ACCESS
  • 1. ZOMBIES!RETAAAAAIN! RETAAAAAIN RETAAAAIN!
  • I will never, ever use Comic Sans in apresentation again.I will never, ever use Comic Sans in apresentation again.I will never, ever use Comic Sans in apresentation again.I will never, ever use Comic Sans in apresentation again.I will never, ever use Comic Sans in apresentation again.I will never, ever use Comic Sans in apresentation again.I will never, ever use Comic Sans in apresentation again.
  • 1. ZOMBIES!Once retainCount hits zero, an object is typicallydeallocated. Call a method on a pointer to that objectand BOOM.When zombies are enabled, deallocated objects’ classis changed to _NSZombie and memory isn’t freed.Subsequent messages to the zombie object cause anice, predictable, break-pointable exception.
  • In Xcode 3, add the environment variable in the Executable Info window
  • In Xcode 4, add the environment variable in the Scheme Editor (⌥ + “Play” Button)
  • HE MADE A COMMONMISTAKE ...
  • DON’T DO THIS NSZombieEnabled=1
  • 2. NSAssert()Call this everywhere you make an assumption aboutthe state of the world.Expect a parameter to be non-nil or non-negative?NSAssert that shit.Fall through a switch statement on an HTTP resultcode? NSAssert that shit.Get a return code from a system function you don’texplicitly handle? NSAssert that shit.
  • NS_BLOCK_ASSERTIONS
  • BUT WAIT...
  • So yeah, you don’t want to ship an app withNSAssert enabled, but it’d be cool if at least some ofyour test builds had them.Because, in the words of Von Moltke the Elder:“No code survives first contact with users.”
  • Of course, if your app asserts out when it’s notconnected to the debugger, BOOM. Users hate that shit.
  • REAL USERHATING THAT SHIT
  • 3. CUSTOM ASSERT HANDLERS So Simple. Just subclass NSAssertionHandler...- (void)handleFailureInFunction:(NSString *)functionName file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ... { va_list args; va_start(args, format); [self showAssertUIAlert:functionName file:fileName lineNumber:line description:format args:args];}- (void)handleFailureInMethod:(SEL)selector object:(id)object file:(NSString *)fileName lineNumber:(NSInteger)line description:(NSString *)format, ... { va_list args; va_start(args, format); NSString *methodName = NSStringFromSelector(selector); [self showAssertUIAlert:methodName file:fileName lineNumber:line description:format args:args];}
  • 4. BREAKPOINT ACTIONSI don’t want to start a religious war here, but I never, everuse NSLog() for debugging.But I do trace like a mofo. All the time. Using breakpointactions!
  • 5. objc_exception_throw()Set a breakpoint on this function in the DEBUGconfiguration/scheme.In every project.Always.
  • XCODE 3
  • XCODE 4
  • RUNS WITHobjc_exception_throw BREAKPOINT
  • DOESN’T
  • 6. CUSTOM EXCEPTION HANDLERSSo what about when we ship our app?Crash reports are a great resource, especially on iOS,since Apple collects them when devices are synced.But wouldn’t it be great if you could catch crashes beforethey happen, save the user’s data, and perhaps even postthe crash to your handy crash reporting web service?
  • IT’S PRETTY FLIPPIN’ EASY! void InstallUncaughtExceptionHandler() { NSSetUncaughtExceptionHandler(&HandleException); signal(SIGABRT, SignalHandler); signal(SIGILL, SignalHandler); signal(SIGSEGV, SignalHandler); signal(SIGFPE, SignalHandler); signal(SIGBUS, SignalHandler); signal(SIGPIPE, SignalHandler); } void SignalHandler(int signal) { // do whatever you want here, save the user’s work, // post a crash report to your web service, show // a dialog apologizing for your shoddy workmanship... whatever! } void HandleException(NSException *exception) { // Unhandled exception no more! } Shamelessly lifted from http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html
  • ESSENTIAL RESOURCES iOS Debugging Magic TN2239 Understanding and Analyzing iOS Application Crash Reports TN2151 Debugging Autorelease Custom Uncaught Exception Handler
  • art@tapsquare.com @artgillespie