Property And Memory Management
          in Objective-C



                      By Yoyo Chen
AGENDA
•   1.Property
•   2.Object ownership
•   3.Memory Management
PROPERTY
•   Objective-C properties ;
    a way to encapsulate an object’s values




         Ref: http://rypress.com/tutorials/objective-c/properties.html
Property Attribute

Attribute            Usage
Accessor             Getter/Setter method
Writablity           readwrite, readonly
Semantics            Without ARC retain/assign/copy
                     With ARC strong/weak/copy/unsafe_unretained
Atomicity            atomic / nonatomic;    Default:atomic
ACCESSOR
• Use Accessor Methods to Get or Set Property Values
• Accessor methods are synthesized automatically by the compiler



@property float heightInMeters;
@synthesize heightInMeters;
Getter method : heightInMeters
Setter method : setHeightInMeters
[person setHeightInMeters:1.9];
float height = [person heightInMeters];
DOT SYNTAX
•   A concise Alternative to Accessor Method Calls
•   float height = person.heightInMeters;
•   person.heightInMeters = 1.9;
Atomic v.s nonatomic (1)
    Atomic
•      Is default behavior
•      Use in multi-threaded code


    Non-Atomic
•     Make setter method faster than atomic
•     Not thread safe
•     May result in unexpected behavior, when two different process access the same
    variable at the same time


Advice: I don’t think you will write multi-threaded code anytime soon. And when you do, I
don’t think atomic setter methods are going to help.



    1.Objective-C Programming: The Big Nerd Ranch Guide :page 220
    2. http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties
Atomic v.s nonatomic (2)

Advice:
I don’t think you will write multi-threaded code anytime soon.
And when you do, I don’t think atomic setter methods are going to help.




   Objective-C Programming: The Big Nerd Ranch Guide :page 220
• Memory Management
1.MRC(manual reference counting)
2.ARC(automatic reference counting)
http://en.wikipedia.org/wiki/Automatic_Reference_Counting#Property_Declarations
MRC       (Manual Reference Counting)




https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMg
Automatic Reference Counting (ARC)
•   A compiler feature that provides automatic memory management of Objective-C objects.
•    Rather than having to think about retain and release operations, ARC allows you to
    concentrate on the interesting code.
•   ARC is supported in Xcode 4.2 or later,
    Mac OS X 10.6 or later, and iOS 4.0 or later(partially support), .
Transitioning to Automatic Reference Counting (ARC)




https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introdu
ARC       (Automatic Referencing Counting)

•   Pointers keep objects alive
•   If fisrName is a strong pointer , firstName is the owner of @”Ray” object
•                  weak pointer,               not the owner




          Ref:http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
EX.
•   Revise First Name
•   Click update




http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Encaps
• Zeroing Weak References
  Only available in Mac OS X ≥ 10.7 and iOS ≥ 5




     Ref:http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
• Avoid Retain Cycle




      Ref :http://ios.sodas.tw/ntumpp-2012f/files/Lecture-2-iOS-in-Developers-view.pdf
• Avoid Retain Cycle




      Ref :http://ios.sodas.tw/ntumpp-2012f/files/Lecture-2-iOS-in-Developers-view.pdf
Ref :http://ios.sodas.tw/ntumpp-2012f/files/Lecture-2-iOS-in-Developers-view.pdf
ARC limitation
       •   ARC does not automate malloc/free, management of the lifetime of Core Foundation
           objects, file descriptors, and so on, you still free such resources by writing
           a dealloc method.




http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduct
DEMO
•   Memory leak: retain cycle
Objective-C Programming: The Big Nerd Ranch Guide :page 125
•   Product  Profiler ->




http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Mem
Property and MM with ARC in Objective-C

Property and MM with ARC in Objective-C

  • 1.
    Property And MemoryManagement in Objective-C By Yoyo Chen
  • 2.
    AGENDA • 1.Property • 2.Object ownership • 3.Memory Management
  • 3.
    PROPERTY • Objective-C properties ; a way to encapsulate an object’s values Ref: http://rypress.com/tutorials/objective-c/properties.html
  • 4.
    Property Attribute Attribute Usage Accessor Getter/Setter method Writablity readwrite, readonly Semantics Without ARC retain/assign/copy With ARC strong/weak/copy/unsafe_unretained Atomicity atomic / nonatomic; Default:atomic
  • 5.
    ACCESSOR • Use AccessorMethods to Get or Set Property Values • Accessor methods are synthesized automatically by the compiler @property float heightInMeters; @synthesize heightInMeters; Getter method : heightInMeters Setter method : setHeightInMeters [person setHeightInMeters:1.9]; float height = [person heightInMeters];
  • 6.
    DOT SYNTAX • A concise Alternative to Accessor Method Calls • float height = person.heightInMeters; • person.heightInMeters = 1.9;
  • 7.
    Atomic v.s nonatomic(1) Atomic • Is default behavior • Use in multi-threaded code Non-Atomic • Make setter method faster than atomic • Not thread safe • May result in unexpected behavior, when two different process access the same variable at the same time Advice: I don’t think you will write multi-threaded code anytime soon. And when you do, I don’t think atomic setter methods are going to help. 1.Objective-C Programming: The Big Nerd Ranch Guide :page 220 2. http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties
  • 8.
    Atomic v.s nonatomic(2) Advice: I don’t think you will write multi-threaded code anytime soon. And when you do, I don’t think atomic setter methods are going to help. Objective-C Programming: The Big Nerd Ranch Guide :page 220
  • 9.
    • Memory Management 1.MRC(manualreference counting) 2.ARC(automatic reference counting)
  • 10.
  • 11.
    MRC (Manual Reference Counting) https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMg
  • 12.
    Automatic Reference Counting(ARC) • A compiler feature that provides automatic memory management of Objective-C objects. • Rather than having to think about retain and release operations, ARC allows you to concentrate on the interesting code. • ARC is supported in Xcode 4.2 or later, Mac OS X 10.6 or later, and iOS 4.0 or later(partially support), .
  • 13.
    Transitioning to AutomaticReference Counting (ARC) https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introdu
  • 14.
    ARC (Automatic Referencing Counting) • Pointers keep objects alive • If fisrName is a strong pointer , firstName is the owner of @”Ray” object • weak pointer, not the owner Ref:http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
  • 15.
  • 16.
    Revise First Name
  • 17.
    Click update http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Encaps
  • 18.
    • Zeroing WeakReferences Only available in Mac OS X ≥ 10.7 and iOS ≥ 5 Ref:http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
  • 19.
    • Avoid RetainCycle Ref :http://ios.sodas.tw/ntumpp-2012f/files/Lecture-2-iOS-in-Developers-view.pdf
  • 20.
    • Avoid RetainCycle Ref :http://ios.sodas.tw/ntumpp-2012f/files/Lecture-2-iOS-in-Developers-view.pdf
  • 21.
  • 22.
    ARC limitation • ARC does not automate malloc/free, management of the lifetime of Core Foundation objects, file descriptors, and so on, you still free such resources by writing a dealloc method. http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduct
  • 24.
    DEMO • Memory leak: retain cycle
  • 25.
    Objective-C Programming: TheBig Nerd Ranch Guide :page 125
  • 26.
    Product  Profiler -> http://developer.apple.com/library/ios/#documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/Mem