High Quality iOS
                           Development


                                       @robinlu
                                       IN-SRC Studio


Saturday, April 9, 2011
Quality



Saturday, April 9, 2011
Topics
                      • Memory management
                      • Tools
                      • Crash Reports
                      • Test



Saturday, April 9, 2011
Memory Management



Saturday, April 9, 2011
Memory Management

                           alloc          object
                                   +1               -1
                           copy         reference        release
                          retain          count

                                           0

                                         dealloc



Saturday, April 9, 2011
Ownership
                          Declare ownership      Release ownership
                          • create / copy       • don’t need anymore
                          • instance variable   • dealloc of owner
                          • container
                          • multi-thread
                          • ...

Saturday, April 9, 2011
Autorelease
                      • give the ownership to autorelease pool
                      • default autorelease pool created by AppKit
                       • life cycle = event cycle
                      • You can create your own autorelease pool
                      • Don’t over use autorelease


Saturday, April 9, 2011
Problems
                      • Memory Leak
                      • Over-release




Saturday, April 9, 2011
Rules
                           You only release or autorelease
                                  objects you own
                  • If you own the object by alloc, copy or
                          retain, you have to release or autorelease
                  • If the object is not owned by you, don’t call
                          release or autorelease




Saturday, April 9, 2011
Pitfalls
                      • Implicit Onwership
                      • Circular Reference




Saturday, April 9, 2011
Implicit Ownership
                  • NSTimer
                  • Delayed performance of selector
                  • Thread
                  • IBOutlet



Saturday, April 9, 2011
Circular Reference
                          A                       A retains B
                                                  B retains C
                                                  C retains A
                  C             B


                              Break it with weak references



Saturday, April 9, 2011
General Practices
                      • Instance Variables
                      • Local Variables




Saturday, April 9, 2011
Instance Variable
            get ownership in             release ownership in
                           init                dealloc

                                           viewDidUnload
                   viewDidLoad
                                               dealloc

                                               setter
                          setter
                                               dealloc

Saturday, April 9, 2011
Local Variable

                 Always release or autorelease in the same scope




Saturday, April 9, 2011
Summary
                      • Ownership
                      • Rules
                      • Pitfalls
                      • Practices



Saturday, April 9, 2011
Analyze



Saturday, April 9, 2011
Analyze
                      • Clang Static Analyzer
                      • More powerful than compiler warnings
                      • Bug finder
                       • memory leaks
                       • dead store
                       • logic error
                       • ...
Saturday, April 9, 2011
Memory Leaks




Saturday, April 9, 2011
Potential Memory Leak




Saturday, April 9, 2011
Dead Store




         Value stored to ‘matchedRange’ during initialization is
                              never read

Saturday, April 9, 2011
Logic error




Saturday, April 9, 2011
Run Analyze




Saturday, April 9, 2011
Instruments



Saturday, April 9, 2011
Instruments
                      • Memory
                      • Performance
                      • ...




Saturday, April 9, 2011
Memory
                      • Memory Leaks
                      • Abnormal Allocation
                       • Heapshots
                      • Over Release
                       • Zombies
                      • Memory Clean up
                       • VM Tracker
Saturday, April 9, 2011
Time Profiling
                      • Sampling
                      • Record Mode
                       • Immediate mode
                       • Deferred mode



Saturday, April 9, 2011
Crash Reports



Saturday, April 9, 2011
Crash Reports

                      • What’s crash reports?
                      • Where to get crash reports?
                      • How to read crash reports?
                      • Generate crash reports by 3rd party lib

Saturday, April 9, 2011
Saturday, April 9, 2011
Xcode Organizer




                          Get crash reports from devices


Saturday, April 9, 2011
Local Directories
                      •   Mac OS X: ~/Library/Logs/CrashReporter/
                          MobileDevice/ <DEVICE_NAME>

                      •   Windows XP: C:Documents and Settings
                          <USERNAME>Application DataApple Computer
                          LogsCrashReporterMobileDevice
                          <DEVICE_NAME>

                      •   Windows Vista + 7: C:Users<USERNAME>
                          AppDataRoamingApple ComputerLogs
                          CrashReporterMobileDevice<DEVICE_NAME>



Saturday, April 9, 2011
iTunes Connect




                          Get users’ crash reports after release


Saturday, April 9, 2011
Crash Types
                  • EXC_BAD_ACCESS (SIGBUS or SIGSEGV)
                  • EXC_CRASH (SIGABRT)
                  • Low Memory
                  • 00000020
                          Exception Codes        Type
                            0x8badf00d         Timeout
                            0xdeadfa11      User Force-Quit


Saturday, April 9, 2011
Symbols
                      • Keep symbol before each release
                       • Xcode Archive
                       • dSYM file
                      • Read the report
                       • Xcode organizer
                       • atos, symbolicatecrash, symbolizecrashlog
                       • 3rd party tools - Symbolicator
Saturday, April 9, 2011
Test



Saturday, April 9, 2011
Test
                      • Unit Test
                      • Beta Test
                      • Automating UI Test




Saturday, April 9, 2011
Unit Test
                      • Unit Test Target
                      • How easy to test: M > C > V




Saturday, April 9, 2011
Automating UI Test
                      • Instruments
                      • Accessibility
                      • Javascript




Saturday, April 9, 2011
Beta Test
                      • Ad Hoc
                      • Build suggestion:
                          Ad Hoc and Distribution share the same
                          bundle id but different from Debug build




Saturday, April 9, 2011
Ad Hoc Deploy
                      • Deploy via internet
                       • Hockey
                       • TestFlightApp.com
                       • dropbox



Saturday, April 9, 2011
Crash Report Lib
                   • plcrashreporter
                    • http://code.google.com/p/plcrashreporter/
                    • can be used in both ad hoc and distribution
                            builds
                          • output as protobuf-encoded messages


Saturday, April 9, 2011
Thanks!



Saturday, April 9, 2011

High quality iOS development

  • 1.
    High Quality iOS Development @robinlu IN-SRC Studio Saturday, April 9, 2011
  • 2.
  • 3.
    Topics • Memory management • Tools • Crash Reports • Test Saturday, April 9, 2011
  • 4.
  • 5.
    Memory Management alloc object +1 -1 copy reference release retain count 0 dealloc Saturday, April 9, 2011
  • 6.
    Ownership Declare ownership Release ownership • create / copy • don’t need anymore • instance variable • dealloc of owner • container • multi-thread • ... Saturday, April 9, 2011
  • 7.
    Autorelease • give the ownership to autorelease pool • default autorelease pool created by AppKit • life cycle = event cycle • You can create your own autorelease pool • Don’t over use autorelease Saturday, April 9, 2011
  • 8.
    Problems • Memory Leak • Over-release Saturday, April 9, 2011
  • 9.
    Rules You only release or autorelease objects you own • If you own the object by alloc, copy or retain, you have to release or autorelease • If the object is not owned by you, don’t call release or autorelease Saturday, April 9, 2011
  • 10.
    Pitfalls • Implicit Onwership • Circular Reference Saturday, April 9, 2011
  • 11.
    Implicit Ownership • NSTimer • Delayed performance of selector • Thread • IBOutlet Saturday, April 9, 2011
  • 12.
    Circular Reference A A retains B B retains C C retains A C B Break it with weak references Saturday, April 9, 2011
  • 13.
    General Practices • Instance Variables • Local Variables Saturday, April 9, 2011
  • 14.
    Instance Variable get ownership in release ownership in init dealloc viewDidUnload viewDidLoad dealloc setter setter dealloc Saturday, April 9, 2011
  • 15.
    Local Variable Always release or autorelease in the same scope Saturday, April 9, 2011
  • 16.
    Summary • Ownership • Rules • Pitfalls • Practices Saturday, April 9, 2011
  • 17.
  • 18.
    Analyze • Clang Static Analyzer • More powerful than compiler warnings • Bug finder • memory leaks • dead store • logic error • ... Saturday, April 9, 2011
  • 19.
  • 20.
  • 21.
    Dead Store Value stored to ‘matchedRange’ during initialization is never read Saturday, April 9, 2011
  • 22.
  • 23.
  • 24.
  • 25.
    Instruments • Memory • Performance • ... Saturday, April 9, 2011
  • 26.
    Memory • Memory Leaks • Abnormal Allocation • Heapshots • Over Release • Zombies • Memory Clean up • VM Tracker Saturday, April 9, 2011
  • 27.
    Time Profiling • Sampling • Record Mode • Immediate mode • Deferred mode Saturday, April 9, 2011
  • 28.
  • 29.
    Crash Reports • What’s crash reports? • Where to get crash reports? • How to read crash reports? • Generate crash reports by 3rd party lib Saturday, April 9, 2011
  • 30.
  • 31.
    Xcode Organizer Get crash reports from devices Saturday, April 9, 2011
  • 32.
    Local Directories • Mac OS X: ~/Library/Logs/CrashReporter/ MobileDevice/ <DEVICE_NAME> • Windows XP: C:Documents and Settings <USERNAME>Application DataApple Computer LogsCrashReporterMobileDevice <DEVICE_NAME> • Windows Vista + 7: C:Users<USERNAME> AppDataRoamingApple ComputerLogs CrashReporterMobileDevice<DEVICE_NAME> Saturday, April 9, 2011
  • 33.
    iTunes Connect Get users’ crash reports after release Saturday, April 9, 2011
  • 34.
    Crash Types • EXC_BAD_ACCESS (SIGBUS or SIGSEGV) • EXC_CRASH (SIGABRT) • Low Memory • 00000020 Exception Codes Type 0x8badf00d Timeout 0xdeadfa11 User Force-Quit Saturday, April 9, 2011
  • 35.
    Symbols • Keep symbol before each release • Xcode Archive • dSYM file • Read the report • Xcode organizer • atos, symbolicatecrash, symbolizecrashlog • 3rd party tools - Symbolicator Saturday, April 9, 2011
  • 36.
  • 37.
    Test • Unit Test • Beta Test • Automating UI Test Saturday, April 9, 2011
  • 38.
    Unit Test • Unit Test Target • How easy to test: M > C > V Saturday, April 9, 2011
  • 39.
    Automating UI Test • Instruments • Accessibility • Javascript Saturday, April 9, 2011
  • 40.
    Beta Test • Ad Hoc • Build suggestion: Ad Hoc and Distribution share the same bundle id but different from Debug build Saturday, April 9, 2011
  • 41.
    Ad Hoc Deploy • Deploy via internet • Hockey • TestFlightApp.com • dropbox Saturday, April 9, 2011
  • 42.
    Crash Report Lib • plcrashreporter • http://code.google.com/p/plcrashreporter/ • can be used in both ad hoc and distribution builds • output as protobuf-encoded messages Saturday, April 9, 2011
  • 43.