SlideShare a Scribd company logo
DEBUGGING
OR HOW I LEARNED TO STOP WORRYING AND LOVE
           EXC_BAD_ACCESS
1. ZOMBIES!

RETAAAAAIN!

               RETAAAAAIN




              RETAAAAIN!
I will never,   ever use   Comic Sans in a
presentation    again.
I will never,   ever use   Comic Sans in a
presentation    again.
I will never,   ever use   Comic Sans in a
presentation    again.
I will never,   ever use   Comic Sans in a
presentation    again.
I will never,   ever use   Comic Sans in a
presentation    again.
I will never,   ever use   Comic Sans in a
presentation    again.
I will never,   ever use   Comic Sans in a
presentation    again.
1. ZOMBIES!
Once retainCount hits zero, an object is typically
deallocated. Call a method on a pointer to that object
and BOOM.

When zombies are enabled, deallocated objects’ class
is changed to _NSZombie and memory isn’t freed.
Subsequent messages to the zombie object cause a
nice, 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 COMMON
MISTAKE ...
DON’T DO THIS
  NSZombieEnabled=1
2. NSAssert()
Call this everywhere you make an assumption about
the 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 result
code? NSAssert that shit.

Get a return code from a system function you don’t
explicitly handle? NSAssert that shit.
NS_BLOCK_ASSERTIONS
BUT WAIT...
So yeah, you don’t want to ship an app with
NSAssert enabled, but it’d be cool if at least some of
your 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 not
connected to the debugger, BOOM.




                 Users hate that shit.
REAL USER
HATING 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
              ACTIONS
I don’t want to start a religious war here, but I never, ever
use NSLog() for debugging.

But I do trace like a mofo. All the time. Using breakpoint
actions!
5. objc_exception_throw()
Set a breakpoint on this function in the DEBUG
configuration/scheme.

In every project.

Always.
XCODE 3
XCODE 4
RUNS WITH
objc_exception_throw
    BREAKPOINT
DOESN’T
6. CUSTOM EXCEPTION
       HANDLERS
So 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 before
they happen, save the user’s data, and perhaps even post
the 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

More Related Content

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

Sony C#/.NET component set analysis
Sony C#/.NET component set analysisSony C#/.NET component set analysis
Sony C#/.NET component set analysis
PVS-Studio
 
Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Peter Higgins
 
Checking VirtualDub
Checking VirtualDubChecking VirtualDub
Checking VirtualDub
Andrey Karpov
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
Janie Clayton
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Andrey Karpov
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java Programming
Katy Allen
 
Custom view
Custom viewCustom view
Custom view
SV.CO
 
Discussing Errors in Unity3D's Open-Source Components
Discussing Errors in Unity3D's Open-Source ComponentsDiscussing Errors in Unity3D's Open-Source Components
Discussing Errors in Unity3D's Open-Source Components
PVS-Studio
 
How to make a large C++-code base manageable
How to make a large C++-code base manageableHow to make a large C++-code base manageable
How to make a large C++-code base manageable
corehard_by
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
Janie Clayton
 
Analyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-StudioAnalyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-Studio
PVS-Studio
 
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source CodeA Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
PVS-Studio
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggy
Andrey Karpov
 
PVS-Studio delved into the FreeBSD kernel
PVS-Studio delved into the FreeBSD kernelPVS-Studio delved into the FreeBSD kernel
PVS-Studio delved into the FreeBSD kernel
PVS-Studio
 
Top 10 bugs in C++ open source projects, checked in 2016
Top 10 bugs in C++ open source projects, checked in 2016Top 10 bugs in C++ open source projects, checked in 2016
Top 10 bugs in C++ open source projects, checked in 2016
PVS-Studio
 
A fresh eye on Oracle VM VirtualBox
A fresh eye on Oracle VM VirtualBoxA fresh eye on Oracle VM VirtualBox
A fresh eye on Oracle VM VirtualBox
PVS-Studio
 
Raising the Bar on Robotics Code Quality
Raising the Bar on Robotics Code QualityRaising the Bar on Robotics Code Quality
Raising the Bar on Robotics Code Quality
Thomas Moulard
 
Errors detected in the Visual C++ 2012 libraries
Errors detected in the Visual C++ 2012 librariesErrors detected in the Visual C++ 2012 libraries
Errors detected in the Visual C++ 2012 libraries
PVS-Studio
 
Re-checking the ReactOS project - a large report
Re-checking the ReactOS project - a large reportRe-checking the ReactOS project - a large report
Re-checking the ReactOS project - a large report
PVS-Studio
 
Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?
PVS-Studio
 

Similar to Debugging: Or How I Learned To Stop Worrying and Love EXC_BAD_ACCESS (20)

Sony C#/.NET component set analysis
Sony C#/.NET component set analysisSony C#/.NET component set analysis
Sony C#/.NET component set analysis
 
Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.Your Library Sucks, and why you should use it.
Your Library Sucks, and why you should use it.
 
Checking VirtualDub
Checking VirtualDubChecking VirtualDub
Checking VirtualDub
 
Thinking In Swift
Thinking In SwiftThinking In Swift
Thinking In Swift
 
Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...Consequences of using the Copy-Paste method in C++ programming and how to dea...
Consequences of using the Copy-Paste method in C++ programming and how to dea...
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java Programming
 
Custom view
Custom viewCustom view
Custom view
 
Discussing Errors in Unity3D's Open-Source Components
Discussing Errors in Unity3D's Open-Source ComponentsDiscussing Errors in Unity3D's Open-Source Components
Discussing Errors in Unity3D's Open-Source Components
 
How to make a large C++-code base manageable
How to make a large C++-code base manageableHow to make a large C++-code base manageable
How to make a large C++-code base manageable
 
Robots in Swift
Robots in SwiftRobots in Swift
Robots in Swift
 
Analyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-StudioAnalyzing the Blender project with PVS-Studio
Analyzing the Blender project with PVS-Studio
 
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source CodeA Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
A Unicorn Seeking Extraterrestrial Life: Analyzing SETI@home's Source Code
 
Why Windows 8 drivers are buggy
Why Windows 8 drivers are buggyWhy Windows 8 drivers are buggy
Why Windows 8 drivers are buggy
 
PVS-Studio delved into the FreeBSD kernel
PVS-Studio delved into the FreeBSD kernelPVS-Studio delved into the FreeBSD kernel
PVS-Studio delved into the FreeBSD kernel
 
Top 10 bugs in C++ open source projects, checked in 2016
Top 10 bugs in C++ open source projects, checked in 2016Top 10 bugs in C++ open source projects, checked in 2016
Top 10 bugs in C++ open source projects, checked in 2016
 
A fresh eye on Oracle VM VirtualBox
A fresh eye on Oracle VM VirtualBoxA fresh eye on Oracle VM VirtualBox
A fresh eye on Oracle VM VirtualBox
 
Raising the Bar on Robotics Code Quality
Raising the Bar on Robotics Code QualityRaising the Bar on Robotics Code Quality
Raising the Bar on Robotics Code Quality
 
Errors detected in the Visual C++ 2012 libraries
Errors detected in the Visual C++ 2012 librariesErrors detected in the Visual C++ 2012 libraries
Errors detected in the Visual C++ 2012 libraries
 
Re-checking the ReactOS project - a large report
Re-checking the ReactOS project - a large reportRe-checking the ReactOS project - a large report
Re-checking the ReactOS project - a large report
 
Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?Of complicacy of programming, or won't C# save us?
Of complicacy of programming, or won't C# save us?
 

Recently uploaded

Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 

Recently uploaded (20)

Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 

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

  • 1. DEBUGGING OR HOW I LEARNED TO STOP WORRYING AND LOVE EXC_BAD_ACCESS
  • 2. 1. ZOMBIES! RETAAAAAIN! RETAAAAAIN RETAAAAIN!
  • 3. I will never, ever use Comic Sans in a presentation again. I will never, ever use Comic Sans in a presentation again. I will never, ever use Comic Sans in a presentation again. I will never, ever use Comic Sans in a presentation again. I will never, ever use Comic Sans in a presentation again. I will never, ever use Comic Sans in a presentation again. I will never, ever use Comic Sans in a presentation again.
  • 4. 1. ZOMBIES! Once retainCount hits zero, an object is typically deallocated. Call a method on a pointer to that object and BOOM. When zombies are enabled, deallocated objects’ class is changed to _NSZombie and memory isn’t freed. Subsequent messages to the zombie object cause a nice, predictable, break-pointable exception.
  • 5. In Xcode 3, add the environment variable in the Executable Info window
  • 6. In Xcode 4, add the environment variable in the Scheme Editor (⌥ + “Play” Button)
  • 7. HE MADE A COMMON MISTAKE ...
  • 8. DON’T DO THIS NSZombieEnabled=1
  • 9. 2. NSAssert() Call this everywhere you make an assumption about the 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 result code? NSAssert that shit. Get a return code from a system function you don’t explicitly handle? NSAssert that shit.
  • 12. So yeah, you don’t want to ship an app with NSAssert enabled, but it’d be cool if at least some of your test builds had them. Because, in the words of Von Moltke the Elder: “No code survives first contact with users.”
  • 13. Of course, if your app asserts out when it’s not connected to the debugger, BOOM. Users hate that shit.
  • 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.
  • 17. 4. BREAKPOINT ACTIONS I don’t want to start a religious war here, but I never, ever use NSLog() for debugging. But I do trace like a mofo. All the time. Using breakpoint actions!
  • 18.
  • 19. 5. objc_exception_throw() Set a breakpoint on this function in the DEBUG configuration/scheme. In every project. Always.
  • 24. 6. CUSTOM EXCEPTION HANDLERS So 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 before they happen, save the user’s data, and perhaps even post the crash to your handy crash reporting web service?
  • 25. 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
  • 26. ESSENTIAL RESOURCES iOS Debugging Magic TN2239 Understanding and Analyzing iOS Application Crash Reports TN2151 Debugging Autorelease Custom Uncaught Exception Handler

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n