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    ...
1. ZOMBIES!Once retainCount hits zero, an object is typicallydeallocated. Call a method on a pointer to that objectand BOO...
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 n...
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...
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 *...
4. BREAKPOINT              ACTIONSI don’t want to start a religious war here, but I never, everuse NSLog() for debugging.B...
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...
IT’S PRETTY FLIPPIN’ EASY!                  void InstallUncaughtExceptionHandler()                  {                    N...
ESSENTIAL RESOURCES   iOS Debugging Magic TN2239 Understanding and Analyzing iOS Application Crash Reports TN2151      Deb...
art@tapsquare.com @artgillespie
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
Upcoming SlideShare
Loading in …5
×

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

3,045 views

Published on

Slides from my presentation at the March Tucson Cocoaheads meeting.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,045
On SlideShare
0
From Embeds
0
Number of Embeds
41
Actions
Shares
0
Downloads
23
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. DEBUGGINGOR HOW I LEARNED TO STOP WORRYING AND LOVE EXC_BAD_ACCESS
    2. 2. 1. ZOMBIES!RETAAAAAIN! RETAAAAAIN RETAAAAIN!
    3. 3. 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.
    4. 4. 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.
    5. 5. In Xcode 3, add the environment variable in the Executable Info window
    6. 6. In Xcode 4, add the environment variable in the Scheme Editor (⌥ + “Play” Button)
    7. 7. HE MADE A COMMONMISTAKE ...
    8. 8. DON’T DO THIS NSZombieEnabled=1
    9. 9. 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.
    10. 10. NS_BLOCK_ASSERTIONS
    11. 11. BUT WAIT...
    12. 12. 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.”
    13. 13. Of course, if your app asserts out when it’s notconnected to the debugger, BOOM. Users hate that shit.
    14. 14. REAL USERHATING THAT SHIT
    15. 15. 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];}
    16. 16. 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!
    17. 17. 5. objc_exception_throw()Set a breakpoint on this function in the DEBUGconfiguration/scheme.In every project.Always.
    18. 18. XCODE 3
    19. 19. XCODE 4
    20. 20. RUNS WITHobjc_exception_throw BREAKPOINT
    21. 21. DOESN’T
    22. 22. 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?
    23. 23. 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
    24. 24. ESSENTIAL RESOURCES iOS Debugging Magic TN2239 Understanding and Analyzing iOS Application Crash Reports TN2151 Debugging Autorelease Custom Uncaught Exception Handler
    25. 25. art@tapsquare.com @artgillespie

    ×