SlideShare une entreprise Scribd logo
1  sur  61
Télécharger pour lire hors ligne
Automatic
Reference Counting
    Giuseppe Arici


     Pragma Night @ Talent Garden
Memory Management
• Manual Reference Counting
 •   Higher level abstraction than malloc / free

 •   Straightforward approach, but must adhere to conventions and rules


• Garbage Collection
 •   Only available for OS X 10.5+, but depracated from 10.8+

 •   Not available on iOS due to performance concerns


• Automatic Reference Counting (ARC)
 •   Makes memory management the job of the compiler (and runtime)

 •   Available for: partially iOS 4+ or OS X 10.6+ / fully iOS 5+ or OS X 10.7+


                                                                                  Pragma Night
Manual Reference Counting




               Reference Counting

                              Pragma Night
Manual Reference Counting
(Only) Objective-C objects are reference counted:

•   Objects start with retain count of 1 when created

•   Increased with retain

•   Decreased with release, autorelease

•   When count equals 0, runtime invokes dealloc


          1               2               1              0

  alloc          retain         release        release

                                               dealloc
                                                     Pragma Night
Objects you create
    For objects you create with [[SomeClass alloc] init] or
    [myInstance copy] (without autoreleasing):

•   Retain should not need to be called

•   Release when you are done using it in the {code block}

- (void)someMethod {
    NSObject *localObject = [[NSObject alloc] init];
    _instanceArray = [[NSArray alloc] initWithObjects:localObject, nil];
    [localObject release];
}

- (void)dealloc {
    [_instanceVariable release];
    [super dealloc];
}




                                                                           Pragma Night
Objects you don’t create
    For objects you don’t create (e.g. get from methods):


•   Retain only when saving to instance (or static) variable

•   Release only if you retained it by saving it (as in above case)

- (void)someMethod {
    id localObject = [anArray objectAtIndex:0];
    _instanceVariable = [localObject retain];
}

- (void)dealloc {
    [_instanceVariable release];
    [super dealloc];
}


                                                               Pragma Night
Autorelease
What if you create an object and you are returning it from a
method, how would you be able to release it?
- (NSArray *)objects {
    NSArray *localArray = [[NSArray alloc] init];     ✇
}
    return localArray;
                                                    Leak !

                                                      ☠
- (NSArray *)objects {
    NSArray *localArray = [[NSArray alloc] init];
    return [localArray release];
}                                                   Crash !
- (NSArray *)objects {
    NSArray *localArray = [[NSArray alloc] init];     ☺
    return [localArray autorelease];
}                                                   Enjoy !
                                                        Pragma Night
Autorelease
•   Instead of explicitly releasing something, you mark it for a
    later release

•   An object called autorelease pool manages a set of
    objects to release when the pool is released

•   Add an object to the release pool by calling autorelease

@autoreleasepool {
    // code goes here
}

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// code goes here
[pool release];



                                                           Pragma Night
Autorelease
•   Autorelease is NOT a Garbage Collector !
    It is deterministic ⌚

•   Objects returned from methods are understood to be
    autoreleased if name is not in implicit retained set
    (alloc/new/copy/mutableCopy/init)

•   If you spawn your own thread, you’ll have to create your
    own NSAutoreleasePool

•   Stack based: autorelease pools can be nested


                   Friday Q&A 2011-09-02: Let's Build NSAutoreleasePool
    http://www.mikeash.com/pyblog/friday-qa-2011-09-02-lets-build-nsautoreleasepool.html

                                                                                     Pragma Night
The Memory Management Rule




 Everything that increases the retain count with
alloc, [mutable]copy[WithZone:], new or retain
is in charge of the corresponding [auto]release.

                           From C++ to Objective-C
          http://pierre.chachatelier.fr/programmation/objective-c.php

                                                                        Pragma Night
Automatic Reference Counting

 “Automatic Reference Counting (ARC) in Objective-C
 makes memory management the job of the compiler. By
 enabling ARC with the new Apple LLVM compiler, you
 will never need to type retain or release again,
 dramatically simplifying the development process, while
 reducing crashes and memory leaks. The compiler has a
 complete understanding of your objects, and releases
 each object the instant it is no longer used, so apps run
 as fast as ever, with predictable, smooth performance.”
          (Apple, “iOS 5 for developers” – http://developer.apple.com/technologies/ios5)




                                                                                   Pragma Night
Automatic Reference Counting
•   The Rule is still valid, but it is managed by the compiler

•   No more retain, release, autorelease nor dealloc

•   New lifetime qualifiers for objects, which includes zeroing
    weak references (only available on iOS 5+ & OS X 10.7+)

•   Apple provides a migration tool which is build into Xcode




                                                           Pragma Night
ARC Settings: Project | File




                           Pragma Night
Requirements



• iOS 5+ & OS X 10.7+          Full Support :)
• iOS 4 & OS X 10.6 (64-bit)   No Runtime :(




                                             Pragma Night
How it works !?
   ARC consists of 2 main components




Frontend Compiler         Optimizer




                                       Pragma Night
ARC Frontend Compiler

• For every local variable inserts retain and
  [auto]release appropriately in the block scope { }
• For every owned instance variable inserts release
  appropriately in the dealloc method
• Call the [super dealloc] in dealloc method
• Generates errors when a variable ownership is
  not set correctly


                                                Pragma Night
Method Family / Ownership

         Method Name Family Object Ownership Action

alloc/new/copy/mutableCopy/init Create and have ownership of it

                         retain Take ownership of it

                       release Relinquish it

                        dealloc Dispose of it

               Everything Else No ownership !


 alloc/new/copy/mutableCopy/init   “ The Implicit Retained Set ”

                                                            Pragma Night
ARC Frontend Compiler 1/3
- (void)someMethod {
    Foo *foo = [[Foo alloc] init];
    [foo bar];

}


- (void)someMethod {
    Foo *foo = [[Foo alloc] init];
    [foo bar];
    [foo release];
}


- (void)someMethod {
    Foo *foo = [[Foo alloc] init];
    [foo bar];
    obj_release(foo);
}


                                     Pragma Night
ARC Frontend Compiler 2/3
- (void)someMethod {
    Foo *localFoo = [self foo];
    [localFoo bar];

    self.foo = [Builder newFoo];
    [localFoo bar];
                                            ☠
}
                                          Crash !

- (void)someMethod {
    Foo *localFoo = objc_retain([self foo]);
    [localFoo bar];

    self.foo = [Builder newFoo];
    [localFoo bar];                          ☺
}
    objc_release(localFoo);
                                           Enjoy !
                                                     Pragma Night
ARC Frontend Compiler 3/3
- (Foo *) foo
{
    return _foo;
}



- (Foo *) foo
{
    return [[_foo retain] autorelease];
}



- (Foo *) foo
{
    return objc_retainAutoreleaseReturnValue(_foo);
}


                                                      Pragma Night
ARC Optimizer


• Optimize the retain and release statements by
  removing them if they are inserted multiple times
  by the ARC Frontend Compiler
• Ensures that performance is not affected by calling
  retain and release multiple times




                                                Pragma Night
ARC Optimizer 1/2

        retain
        autorelease
        retain
        release
stack

                        Pragma Night
ARC Optimizer 1/2

        retain


        release
stack

                        Pragma Night
ARC Optimizer 2/2
- (Foo *) foo                              called method
{
    return objc_retainAutoreleaseReturnValue(_foo);
}




                    Autorelease Pool

- (void)someMethod {
    Foo *f = objc_retainAutoreleasedReturnValue([self foo]);
    [f bar];

    self.foo = [Builder newFoo];
    [f bar];
    objc_release(f);
}                                           caller method
                                                        Pragma Night
Variable Ownership Qualifiers


• __strong (default)
• __weak
• __unsafe_unretained
• __autoreleasing
                 Only For Object Type Variables !
                  ( id | AnyClass : NSObject )

                                             Pragma Night
Retain Cycles 1/5
    Parent                      Child
                   __strong

    _view


  +1                          +1
                              _delegate
                   __strong
   Controller                   View
as View Delegate
                                          Pragma Night
Retain Cycles 2/5
    Parent                             Child
                    __strong

    _view


     =                               +1
                                     _delegate
               __unsafe_unretained
   Controller                          View
as View Delegate
                                                 Pragma Night
Retain Cycles 3/5
    Parent                      Child
                   __strong

    _view


     =                        +1
                              _delegate
                   __weak
   Controller                   View
as View Delegate
                                          Pragma Night
Retain Cycles 4/5
dealloc
          Parent                           Child
                         release




      ☠
          _view
                     New with ARC in
                   iOS 5+ & OS X 10.7+
                                           =
                                         _delegate
                         __weak
      Controller                           View      ☺nil
   as View Delegate
                                                     Pragma Night
Retain Cycles 5/5




                    Pragma Night
__strong
/* ARC */
{
    id __strong obj1 = [[NSObject alloc] init];
    id          obj2 = [[NSObject alloc] init];
}



/* not-ARC */
{
    id obj1 = [[NSObject alloc] init];
    [obj1 release];
}



• Strong reference for the object
• Default for ‘id’ and Object Type Variables !
                                                  Pragma Night
__weak
/* ARC */
{
    id __strong obj1 = [[NSObject alloc] init];
    id __weak   obj2 = obj1;
}



/* not-ARC */
{
    id obj1 = [[NSObject alloc] init];
    id obj2 = obj1;
    [obj1 release];
}


• No ownership of the object
• When discarded, the variable is assigned to nil
                                                  Pragma Night
__unsafe_unretained
/* ARC */
{
    id __unsafe_unretained obj = [[NSObject alloc] init];
}




Assigning retained object to unsafe_unretained variable; object will be
      released after assignment [-Warc-unsafe-retained-assign]


• UNSAFE !
• You must take care of the variables manually
• __weak only for iOS 5+ / OSX 10.7+
                                                                Pragma Night
__autoreleasing
 @autoreleasepool
 {
     id __strong obj = [NSMutableArray array];
 }




 - (BOOL) performWithError:(NSError **)error;

 - (BOOL) perfomrWithError:(NSError * __autoreleasing *)error;




• Pointer to ‘id’ or to ‘Object Type Variables Pointer’ is
  qualified with __autoreleasing as default

                                                         Pragma Night
Variables Initialization
 /*   ARC */
 id   __strong obj1;
 id   __weak obj2;
 id   __unsafe_unretained obj3;
 id   __autoreleasing obj4;



 /*   not-ARC */
 id   __strong obj1 = nil;
 id
 id
      __weak obj2 = nil;
      __unsafe_unretained obj3;
                                             ☠
 id   __autoreleasing obj4 = nil;          Crash !

• Any variables that are qualified with __strong,
  __weak and __autoreleasing are initialized with nil

                                                   Pragma Night
Property
Object Property Modifier Variable Ownership Qualifier
                 assign __unsafe_unretained
      unsafe_unretained __unsafe_unretained
                  weak __weak
                 retain __strong
                 strong __strong
                  copy __strong    *

    * Note: new copied object is assigned

                                               Pragma Night
Property


• Same ownership rules as instance variables
• Manually declared instance variables has to have
  the same ownership qualifier as the property
• Copy: copies the object by using copyWithZone


                                                Pragma Night
Coding With ARC Rules


What is forbidden ?
What can be done but with care ?
What is mandatory ?




                                   Pragma Night
Rule 01/12
 • Forget about using retain, release,
     retainCount and autorelease


 {
     id obj1 = [[NSObject alloc] init];
     /* ... */
     [obj1 release];
 }




error: ARC forbids explicit message send of 'release'
     [obj release];

                                                        Pragma Night
Rule 02/12
 • Forget about using NSAllocateObject,
    NSDeallocateObject, NSZone & Co.


 /* NSObject.h */

 /***********!
             Object Allocation / Deallocation! *******/
                                              !
 FOUNDATION_EXPORT id
 NSAllocateObject(Class, NSUInteger, NSZone *)
 NS_AUTOMATED_REFCOUNT_UNAVAILABLE;



error: 'NSAllocateObject' is unavailable:
     not available in automatic reference counting mode

                                                          Pragma Night
Rule 03/12
 • Forget about using NSAutoreleasePool
    Use @autoreleasepool { ... } Instead


 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 /* ... */

 [pool drain]; // [pool release];




error: 'NSAutoreleasePool' is unavailable:
      not available in automatic reference counting mode

                                                           Pragma Night
Rule 04/12
 • Forget about calling super dealloc
     explicitly


 - (void) dealloc
 {
     free(buffer);
     [super dealloc];
 }




error: ARC forbids explicit message send of 'dealloc'
    [super dealloc]; not available in automatic reference counting mode

                                                                Pragma Night
Rule 05/12
 • Object Type Variables Cannot Be
    Members of struct or union in C



 struct Data
 {
     NSMutableArray *array;
 };




error: ARC forbids Objective-C objs in structs or unions
    NSMutableArray *array;

                                                           Pragma Night
Rule 06/12
• Exceptions use must be exceptional
 •   For performance purpose, ARC code leaks on exceptions

 •   In Obj-C ARC is not exception-safe for normal releases by default

 •   -fobjc-arc-exceptions / -fno-objc-arc-exceptions: enable / disable

 •   In Obj-C++ -fobjc-arc-exceptions is enabled by default




                                                                          Pragma Night
Rule 07/12
• IBOutlet must be declared as weak
 •   Outlets should be defined as declared properties

 •   Outlets should generally be weak

 •   Top-level objects (or, in iOS, a storyboard scene) should be strong




                    Resource Programming Guide


                                                                           Pragma Night
Rule 08/12
• Be careful with performSelector


#pragma clang diagnostic push
#pragma clang diagnostic ignored 
    "-Warc-performSelector-leaks"

[self performSelector:@selector(someMethod)];

#pragma clang diagnostic pop




                                                Pragma Night
Rule 09/12
• Follow the naming rule for methods related
   to object creation: implicit retained set
  • If begins { alloc/new/copy/mutableCopy }, the caller has ownership
  • If begins { init }, the method has to: *
    • be an instance method
    • return an object of type ‘id’ or an object of type of its [super|sub]class
    • return an object not registered in autoreleasepool: the caller has ownership
- (id)init...;


             * A special case: “ + (void) initialize ”
                                                                              Pragma Night
False Positive
-(NSString*) copyRightString;




-(NSString*) copyRightString NS_RETURNS_NOT_RETAINED;



• NS_RETURNS_RETAINED
• NS_RETURNS_NOT_RETAINED
This macro is ONLY to be used in exceptional circumstances, not to
   annotate functions which conform to the Cocoa naming rules.

                                                            Pragma Night
Rule 10/12
• Switch cases must be wrapped in scope
   with { ... }



switch (value)
{
    case 0:
    {
        NSObject *obj= [[NSObject alloc] init];
        NSLog(@"obj for value 0: %@", obj);
    }
    break;
}



                                                  Pragma Night
Rule 11/12
• The old style singleton which redefines
   retain and release is not ARC compatible
• Singletons should be redefined with
   dispatch_once

static Foo *sharedInstance;

+ (id)sharedInstance {
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedInstance = [[Foo alloc] init];
    });
    return sharedInstance;
}


                                               Pragma Night
Rule 12/12
 • 'id' and 'void *' Have to Be Cast Explicitly


 id object = [[NSObject alloc] init];
 void *pointer = object;




Error: implicit conversion of a non-Objective-C pointer type 'void *'
      to 'id' is disallowed with ARC

                                                                 Pragma Night
( _bridge cast )

id object1 = [[NSObject alloc] init];
void *pointer = (__bridge void *)object1;
id object2 = (__bridge id)pointer;




• More dangerous than an __unsafe_unretained
   qualified variable !
• You have to manage ownership of the object
   yourself carefully or it crashes

                                               Pragma Night
( __bridge_retained cast )
/* ARC */
id object = [[NSObject alloc] init];
void *pointer = (__bridge_retained void *)object;


/* not-ARC */
id object = [[NSObject alloc] init];
void *pointer = object;
[(id)pointer retain];



• From ARC to Core Foundation
• __bridge_retained cast will retain the object just
   after the assignment is done


                                                    Pragma Night
( __bridge_transfer cast )
/* ARC */
void *pointer = &bytes;
id object = (__bridge_transfer id)pointer;


/* not-ARC */
id object = (id)pointer;
[object retain];
[(id)pointer release];



• From Core Foundation to ARC
• __bridge_transfer cast will release the object just
   after the assignment is done


                                                Pragma Night
Toll-Free Bridging
/* NSObject.h */

// After using a CFBridgingRetain on an NSObject, the caller
// must take responsibility for calling CFRelease at an
// appropriate time

NS_INLINE CF_RETURNS_RETAINED CFTypeRef
CFBridgingRetain(id X)
{
    return (__bridge_retained CFTypeRef)X;
}

NS_INLINE id
CFBridgingRelease(CFTypeRef CF_CONSUMED X)
{
    return (__bridge_transfer id)X;
}



                                                        Pragma Night
ARC MACRO

// define some LLVM3 macros if the code is
// compiled with a different compiler (ie LLVMGCC42)
#ifndef __has_ feature
#define __has_feature(x) 0
#endif

#ifndef __has_extension
// Compatibility with pre-3.0 compilers
#define __has_extension __has_feature
#endif

#if __has_feature(objc_arc) && __clang_major__ >= 3
#define ARC_ENABLED 1
#endif

            Check @ compile-time if ARC is enabled

                                                       Pragma Night
tl;dr                   What Is ARC ?

• Automatic memory management of Obj-C objects
  •   Compiler obeys and enforces existing conventions


• Full interoperability with manual retain and release
• New runtime features
  •   Weak pointers

  •   Advanced performance optimizations




                                                         Pragma Night
tl;dr                 What ARC Is NOT ?

• No new runtime memory model
• No automation for malloc/free, CF, etc.
• No garbage collector
  •   No heap scans

  •   No whole app pauses

  •   No non-deterministic releases




                                            Pragma Night
ARC References

•   Clang: Automatic Reference Counting documentation

•   Apple: Transitioning to ARC Release Notes

•   Apple: Xcode New Features User Guide: Automatic Reference Counting

•   WWDC 2011 – Session 323 – Introducing Automatic Reference Counting

•   WWDC 2012 – Session 406/416 – Adopting Automatic Reference Counting

•   Mike Ash: Friday Q&A about Automatic Reference Counting

•   Mike Ash: Zeroing weak references without ARC




                                                                          Pragma Night
ARC Books




            Pragma Night
NSLog(@”Thank you!”);




  giuseppe.arici@pragmamark.org

                                  Pragma Night

Contenu connexe

Tendances

Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Yuta Iwama
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchMatteo Battaglio
 
Know yourengines velocity2011
Know yourengines velocity2011Know yourengines velocity2011
Know yourengines velocity2011Demis Bellot
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisRuslan Shevchenko
 
Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Peter Maas
 
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Nilesh Jayanandana
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6Solution4Future
 
Building a java tracer
Building a java tracerBuilding a java tracer
Building a java tracerrahulrevo
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMTom Lee
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overviewhesher
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesCharles Nutter
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!José Paumard
 
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMCharles Nutter
 
JavaScript, Beyond the Curly Braces
JavaScript, Beyond the Curly BracesJavaScript, Beyond the Curly Braces
JavaScript, Beyond the Curly BracesChicago ALT.NET
 
Inside the JVM - Follow the white rabbit! / Breizh JUG
Inside the JVM - Follow the white rabbit! / Breizh JUGInside the JVM - Follow the white rabbit! / Breizh JUG
Inside the JVM - Follow the white rabbit! / Breizh JUGSylvain Wallez
 
Asynchronous I/O in Python 3
Asynchronous I/O in Python 3Asynchronous I/O in Python 3
Asynchronous I/O in Python 3Feihong Hsu
 

Tendances (19)

Seeking Clojure
Seeking ClojureSeeking Clojure
Seeking Clojure
 
Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central Dispatch
 
Know yourengines velocity2011
Know yourengines velocity2011Know yourengines velocity2011
Know yourengines velocity2011
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with this
 
Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3
 
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6
 
JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6JavaScript - new features in ECMAScript 6
JavaScript - new features in ECMAScript 6
 
Building a java tracer
Building a java tracerBuilding a java tracer
Building a java tracer
 
Dispatch in Clojure
Dispatch in ClojureDispatch in Clojure
Dispatch in Clojure
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVM
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
ES2015 (ES6) Overview
ES2015 (ES6) OverviewES2015 (ES6) Overview
ES2015 (ES6) Overview
 
JavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for DummiesJavaOne 2011 - JVM Bytecode for Dummies
JavaOne 2011 - JVM Bytecode for Dummies
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
 
JRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVMJRuby 9000 - Optimizing Above the JVM
JRuby 9000 - Optimizing Above the JVM
 
JavaScript, Beyond the Curly Braces
JavaScript, Beyond the Curly BracesJavaScript, Beyond the Curly Braces
JavaScript, Beyond the Curly Braces
 
Inside the JVM - Follow the white rabbit! / Breizh JUG
Inside the JVM - Follow the white rabbit! / Breizh JUGInside the JVM - Follow the white rabbit! / Breizh JUG
Inside the JVM - Follow the white rabbit! / Breizh JUG
 
Asynchronous I/O in Python 3
Asynchronous I/O in Python 3Asynchronous I/O in Python 3
Asynchronous I/O in Python 3
 

En vedette

Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference CountingGiuseppe Arici
 
Auto Layout Under Control @ Pragma conference 2013
Auto Layout Under Control @ Pragma conference 2013Auto Layout Under Control @ Pragma conference 2013
Auto Layout Under Control @ Pragma conference 2013Giuseppe Arici
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - CJussi Pohjolainen
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to SwiftGiordano Scalzo
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming LanguageGiuseppe Arici
 

En vedette (12)

Digital Universitas
Digital UniversitasDigital Universitas
Digital Universitas
 
GDB Mobile
GDB MobileGDB Mobile
GDB Mobile
 
Objective-C @ ITIS
Objective-C @ ITISObjective-C @ ITIS
Objective-C @ ITIS
 
Parte II Objective C
Parte II   Objective CParte II   Objective C
Parte II Objective C
 
Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference Counting
 
Auto Layout Under Control @ Pragma conference 2013
Auto Layout Under Control @ Pragma conference 2013Auto Layout Under Control @ Pragma conference 2013
Auto Layout Under Control @ Pragma conference 2013
 
Swift Introduction
Swift IntroductionSwift Introduction
Swift Introduction
 
Objective-C
Objective-CObjective-C
Objective-C
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
Swift Programming Language
Swift Programming LanguageSwift Programming Language
Swift Programming Language
 

Similaire à Automatic Reference Counting @ Pragma Night

Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCDrsebbe
 
Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Jigar Maheshwari
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript EverywherePascal Rettig
 
Memory management in Objective C
Memory management in Objective CMemory management in Objective C
Memory management in Objective CNeha Gupta
 
JavaScript in 2015
JavaScript in 2015JavaScript in 2015
JavaScript in 2015Igor Laborie
 
Useful and Practical Functionalities in Realm
Useful and Practical Functionalities in RealmUseful and Practical Functionalities in Realm
Useful and Practical Functionalities in RealmYusuke Kita
 
Automatic Reference Counting
Automatic Reference Counting Automatic Reference Counting
Automatic Reference Counting pragmamark
 
iOS 2 - The practical Stuff
iOS 2 - The practical StuffiOS 2 - The practical Stuff
iOS 2 - The practical StuffPetr Dvorak
 
Objective-C Survives
Objective-C SurvivesObjective-C Survives
Objective-C SurvivesS Akai
 
Object Lifetime In C C++
Object Lifetime In C C++Object Lifetime In C C++
Object Lifetime In C C++ppd1961
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOSPetr Dvorak
 
iPhone dev intro
iPhone dev introiPhone dev intro
iPhone dev introVonbo
 
Beginning to iPhone development
Beginning to iPhone developmentBeginning to iPhone development
Beginning to iPhone developmentVonbo
 
ARCでめちゃモテiOSプログラマー
ARCでめちゃモテiOSプログラマーARCでめちゃモテiOSプログラマー
ARCでめちゃモテiOSプログラマーSatoshi Asano
 
Graal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT CompilerGraal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT CompilerKoichi Sakata
 
iPhone Memory Management
iPhone Memory ManagementiPhone Memory Management
iPhone Memory ManagementVadim Zimin
 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2NAILBITER
 

Similaire à Automatic Reference Counting @ Pragma Night (20)

Blocks & GCD
Blocks & GCDBlocks & GCD
Blocks & GCD
 
Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)Blocks and GCD(Grand Central Dispatch)
Blocks and GCD(Grand Central Dispatch)
 
Javascript Everywhere
Javascript EverywhereJavascript Everywhere
Javascript Everywhere
 
Memory management in Objective C
Memory management in Objective CMemory management in Objective C
Memory management in Objective C
 
JavaScript in 2015
JavaScript in 2015JavaScript in 2015
JavaScript in 2015
 
Objective C Memory Management
Objective C Memory ManagementObjective C Memory Management
Objective C Memory Management
 
Useful and Practical Functionalities in Realm
Useful and Practical Functionalities in RealmUseful and Practical Functionalities in Realm
Useful and Practical Functionalities in Realm
 
Automatic Reference Counting
Automatic Reference Counting Automatic Reference Counting
Automatic Reference Counting
 
iOS 2 - The practical Stuff
iOS 2 - The practical StuffiOS 2 - The practical Stuff
iOS 2 - The practical Stuff
 
Objective-C Survives
Objective-C SurvivesObjective-C Survives
Objective-C Survives
 
Spock
SpockSpock
Spock
 
Object Lifetime In C C++
Object Lifetime In C C++Object Lifetime In C C++
Object Lifetime In C C++
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOS
 
iPhone dev intro
iPhone dev introiPhone dev intro
iPhone dev intro
 
Beginning to iPhone development
Beginning to iPhone developmentBeginning to iPhone development
Beginning to iPhone development
 
Racing with Droids
Racing with DroidsRacing with Droids
Racing with Droids
 
ARCでめちゃモテiOSプログラマー
ARCでめちゃモテiOSプログラマーARCでめちゃモテiOSプログラマー
ARCでめちゃモテiOSプログラマー
 
Graal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT CompilerGraal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT Compiler
 
iPhone Memory Management
iPhone Memory ManagementiPhone Memory Management
iPhone Memory Management
 
iPhone Seminar Part 2
iPhone Seminar Part 2iPhone Seminar Part 2
iPhone Seminar Part 2
 

Dernier

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Dernier (20)

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Automatic Reference Counting @ Pragma Night

  • 1. Automatic Reference Counting Giuseppe Arici Pragma Night @ Talent Garden
  • 2. Memory Management • Manual Reference Counting • Higher level abstraction than malloc / free • Straightforward approach, but must adhere to conventions and rules • Garbage Collection • Only available for OS X 10.5+, but depracated from 10.8+ • Not available on iOS due to performance concerns • Automatic Reference Counting (ARC) • Makes memory management the job of the compiler (and runtime) • Available for: partially iOS 4+ or OS X 10.6+ / fully iOS 5+ or OS X 10.7+ Pragma Night
  • 3. Manual Reference Counting Reference Counting Pragma Night
  • 4. Manual Reference Counting (Only) Objective-C objects are reference counted: • Objects start with retain count of 1 when created • Increased with retain • Decreased with release, autorelease • When count equals 0, runtime invokes dealloc 1 2 1 0 alloc retain release release dealloc Pragma Night
  • 5. Objects you create For objects you create with [[SomeClass alloc] init] or [myInstance copy] (without autoreleasing): • Retain should not need to be called • Release when you are done using it in the {code block} - (void)someMethod { NSObject *localObject = [[NSObject alloc] init]; _instanceArray = [[NSArray alloc] initWithObjects:localObject, nil]; [localObject release]; } - (void)dealloc { [_instanceVariable release]; [super dealloc]; } Pragma Night
  • 6. Objects you don’t create For objects you don’t create (e.g. get from methods): • Retain only when saving to instance (or static) variable • Release only if you retained it by saving it (as in above case) - (void)someMethod { id localObject = [anArray objectAtIndex:0]; _instanceVariable = [localObject retain]; } - (void)dealloc { [_instanceVariable release]; [super dealloc]; } Pragma Night
  • 7. Autorelease What if you create an object and you are returning it from a method, how would you be able to release it? - (NSArray *)objects { NSArray *localArray = [[NSArray alloc] init]; ✇ } return localArray; Leak ! ☠ - (NSArray *)objects { NSArray *localArray = [[NSArray alloc] init]; return [localArray release]; } Crash ! - (NSArray *)objects { NSArray *localArray = [[NSArray alloc] init]; ☺ return [localArray autorelease]; } Enjoy ! Pragma Night
  • 8. Autorelease • Instead of explicitly releasing something, you mark it for a later release • An object called autorelease pool manages a set of objects to release when the pool is released • Add an object to the release pool by calling autorelease @autoreleasepool { // code goes here } NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // code goes here [pool release]; Pragma Night
  • 9. Autorelease • Autorelease is NOT a Garbage Collector ! It is deterministic ⌚ • Objects returned from methods are understood to be autoreleased if name is not in implicit retained set (alloc/new/copy/mutableCopy/init) • If you spawn your own thread, you’ll have to create your own NSAutoreleasePool • Stack based: autorelease pools can be nested Friday Q&A 2011-09-02: Let's Build NSAutoreleasePool http://www.mikeash.com/pyblog/friday-qa-2011-09-02-lets-build-nsautoreleasepool.html Pragma Night
  • 10. The Memory Management Rule Everything that increases the retain count with alloc, [mutable]copy[WithZone:], new or retain is in charge of the corresponding [auto]release. From C++ to Objective-C http://pierre.chachatelier.fr/programmation/objective-c.php Pragma Night
  • 11. Automatic Reference Counting “Automatic Reference Counting (ARC) in Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.” (Apple, “iOS 5 for developers” – http://developer.apple.com/technologies/ios5) Pragma Night
  • 12. Automatic Reference Counting • The Rule is still valid, but it is managed by the compiler • No more retain, release, autorelease nor dealloc • New lifetime qualifiers for objects, which includes zeroing weak references (only available on iOS 5+ & OS X 10.7+) • Apple provides a migration tool which is build into Xcode Pragma Night
  • 13. ARC Settings: Project | File Pragma Night
  • 14. Requirements • iOS 5+ & OS X 10.7+ Full Support :) • iOS 4 & OS X 10.6 (64-bit) No Runtime :( Pragma Night
  • 15. How it works !? ARC consists of 2 main components Frontend Compiler Optimizer Pragma Night
  • 16. ARC Frontend Compiler • For every local variable inserts retain and [auto]release appropriately in the block scope { } • For every owned instance variable inserts release appropriately in the dealloc method • Call the [super dealloc] in dealloc method • Generates errors when a variable ownership is not set correctly Pragma Night
  • 17. Method Family / Ownership Method Name Family Object Ownership Action alloc/new/copy/mutableCopy/init Create and have ownership of it retain Take ownership of it release Relinquish it dealloc Dispose of it Everything Else No ownership ! alloc/new/copy/mutableCopy/init “ The Implicit Retained Set ” Pragma Night
  • 18. ARC Frontend Compiler 1/3 - (void)someMethod { Foo *foo = [[Foo alloc] init]; [foo bar]; } - (void)someMethod { Foo *foo = [[Foo alloc] init]; [foo bar]; [foo release]; } - (void)someMethod { Foo *foo = [[Foo alloc] init]; [foo bar]; obj_release(foo); } Pragma Night
  • 19. ARC Frontend Compiler 2/3 - (void)someMethod { Foo *localFoo = [self foo]; [localFoo bar]; self.foo = [Builder newFoo]; [localFoo bar]; ☠ } Crash ! - (void)someMethod { Foo *localFoo = objc_retain([self foo]); [localFoo bar]; self.foo = [Builder newFoo]; [localFoo bar]; ☺ } objc_release(localFoo); Enjoy ! Pragma Night
  • 20. ARC Frontend Compiler 3/3 - (Foo *) foo { return _foo; } - (Foo *) foo { return [[_foo retain] autorelease]; } - (Foo *) foo { return objc_retainAutoreleaseReturnValue(_foo); } Pragma Night
  • 21. ARC Optimizer • Optimize the retain and release statements by removing them if they are inserted multiple times by the ARC Frontend Compiler • Ensures that performance is not affected by calling retain and release multiple times Pragma Night
  • 22. ARC Optimizer 1/2 retain autorelease retain release stack Pragma Night
  • 23. ARC Optimizer 1/2 retain release stack Pragma Night
  • 24. ARC Optimizer 2/2 - (Foo *) foo called method { return objc_retainAutoreleaseReturnValue(_foo); } Autorelease Pool - (void)someMethod { Foo *f = objc_retainAutoreleasedReturnValue([self foo]); [f bar]; self.foo = [Builder newFoo]; [f bar]; objc_release(f); } caller method Pragma Night
  • 25. Variable Ownership Qualifiers • __strong (default) • __weak • __unsafe_unretained • __autoreleasing Only For Object Type Variables ! ( id | AnyClass : NSObject ) Pragma Night
  • 26. Retain Cycles 1/5 Parent Child __strong _view +1 +1 _delegate __strong Controller View as View Delegate Pragma Night
  • 27. Retain Cycles 2/5 Parent Child __strong _view = +1 _delegate __unsafe_unretained Controller View as View Delegate Pragma Night
  • 28. Retain Cycles 3/5 Parent Child __strong _view = +1 _delegate __weak Controller View as View Delegate Pragma Night
  • 29. Retain Cycles 4/5 dealloc Parent Child release ☠ _view New with ARC in iOS 5+ & OS X 10.7+ = _delegate __weak Controller View ☺nil as View Delegate Pragma Night
  • 30. Retain Cycles 5/5 Pragma Night
  • 31. __strong /* ARC */ { id __strong obj1 = [[NSObject alloc] init]; id obj2 = [[NSObject alloc] init]; } /* not-ARC */ { id obj1 = [[NSObject alloc] init]; [obj1 release]; } • Strong reference for the object • Default for ‘id’ and Object Type Variables ! Pragma Night
  • 32. __weak /* ARC */ { id __strong obj1 = [[NSObject alloc] init]; id __weak obj2 = obj1; } /* not-ARC */ { id obj1 = [[NSObject alloc] init]; id obj2 = obj1; [obj1 release]; } • No ownership of the object • When discarded, the variable is assigned to nil Pragma Night
  • 33. __unsafe_unretained /* ARC */ { id __unsafe_unretained obj = [[NSObject alloc] init]; } Assigning retained object to unsafe_unretained variable; object will be released after assignment [-Warc-unsafe-retained-assign] • UNSAFE ! • You must take care of the variables manually • __weak only for iOS 5+ / OSX 10.7+ Pragma Night
  • 34. __autoreleasing @autoreleasepool { id __strong obj = [NSMutableArray array]; } - (BOOL) performWithError:(NSError **)error; - (BOOL) perfomrWithError:(NSError * __autoreleasing *)error; • Pointer to ‘id’ or to ‘Object Type Variables Pointer’ is qualified with __autoreleasing as default Pragma Night
  • 35. Variables Initialization /* ARC */ id __strong obj1; id __weak obj2; id __unsafe_unretained obj3; id __autoreleasing obj4; /* not-ARC */ id __strong obj1 = nil; id id __weak obj2 = nil; __unsafe_unretained obj3; ☠ id __autoreleasing obj4 = nil; Crash ! • Any variables that are qualified with __strong, __weak and __autoreleasing are initialized with nil Pragma Night
  • 36. Property Object Property Modifier Variable Ownership Qualifier assign __unsafe_unretained unsafe_unretained __unsafe_unretained weak __weak retain __strong strong __strong copy __strong * * Note: new copied object is assigned Pragma Night
  • 37. Property • Same ownership rules as instance variables • Manually declared instance variables has to have the same ownership qualifier as the property • Copy: copies the object by using copyWithZone Pragma Night
  • 38. Coding With ARC Rules What is forbidden ? What can be done but with care ? What is mandatory ? Pragma Night
  • 39. Rule 01/12 • Forget about using retain, release, retainCount and autorelease { id obj1 = [[NSObject alloc] init]; /* ... */ [obj1 release]; } error: ARC forbids explicit message send of 'release' [obj release]; Pragma Night
  • 40. Rule 02/12 • Forget about using NSAllocateObject, NSDeallocateObject, NSZone & Co. /* NSObject.h */ /***********! Object Allocation / Deallocation! *******/ ! FOUNDATION_EXPORT id NSAllocateObject(Class, NSUInteger, NSZone *) NS_AUTOMATED_REFCOUNT_UNAVAILABLE; error: 'NSAllocateObject' is unavailable: not available in automatic reference counting mode Pragma Night
  • 41. Rule 03/12 • Forget about using NSAutoreleasePool Use @autoreleasepool { ... } Instead NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; /* ... */ [pool drain]; // [pool release]; error: 'NSAutoreleasePool' is unavailable: not available in automatic reference counting mode Pragma Night
  • 42. Rule 04/12 • Forget about calling super dealloc explicitly - (void) dealloc { free(buffer); [super dealloc]; } error: ARC forbids explicit message send of 'dealloc' [super dealloc]; not available in automatic reference counting mode Pragma Night
  • 43. Rule 05/12 • Object Type Variables Cannot Be Members of struct or union in C struct Data { NSMutableArray *array; }; error: ARC forbids Objective-C objs in structs or unions NSMutableArray *array; Pragma Night
  • 44. Rule 06/12 • Exceptions use must be exceptional • For performance purpose, ARC code leaks on exceptions • In Obj-C ARC is not exception-safe for normal releases by default • -fobjc-arc-exceptions / -fno-objc-arc-exceptions: enable / disable • In Obj-C++ -fobjc-arc-exceptions is enabled by default Pragma Night
  • 45. Rule 07/12 • IBOutlet must be declared as weak • Outlets should be defined as declared properties • Outlets should generally be weak • Top-level objects (or, in iOS, a storyboard scene) should be strong Resource Programming Guide Pragma Night
  • 46. Rule 08/12 • Be careful with performSelector #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" [self performSelector:@selector(someMethod)]; #pragma clang diagnostic pop Pragma Night
  • 47. Rule 09/12 • Follow the naming rule for methods related to object creation: implicit retained set • If begins { alloc/new/copy/mutableCopy }, the caller has ownership • If begins { init }, the method has to: * • be an instance method • return an object of type ‘id’ or an object of type of its [super|sub]class • return an object not registered in autoreleasepool: the caller has ownership - (id)init...; * A special case: “ + (void) initialize ” Pragma Night
  • 48. False Positive -(NSString*) copyRightString; -(NSString*) copyRightString NS_RETURNS_NOT_RETAINED; • NS_RETURNS_RETAINED • NS_RETURNS_NOT_RETAINED This macro is ONLY to be used in exceptional circumstances, not to annotate functions which conform to the Cocoa naming rules. Pragma Night
  • 49. Rule 10/12 • Switch cases must be wrapped in scope with { ... } switch (value) { case 0: { NSObject *obj= [[NSObject alloc] init]; NSLog(@"obj for value 0: %@", obj); } break; } Pragma Night
  • 50. Rule 11/12 • The old style singleton which redefines retain and release is not ARC compatible • Singletons should be redefined with dispatch_once static Foo *sharedInstance; + (id)sharedInstance { static dispatch_once_t predicate; dispatch_once(&predicate, ^{ sharedInstance = [[Foo alloc] init]; }); return sharedInstance; } Pragma Night
  • 51. Rule 12/12 • 'id' and 'void *' Have to Be Cast Explicitly id object = [[NSObject alloc] init]; void *pointer = object; Error: implicit conversion of a non-Objective-C pointer type 'void *' to 'id' is disallowed with ARC Pragma Night
  • 52. ( _bridge cast ) id object1 = [[NSObject alloc] init]; void *pointer = (__bridge void *)object1; id object2 = (__bridge id)pointer; • More dangerous than an __unsafe_unretained qualified variable ! • You have to manage ownership of the object yourself carefully or it crashes Pragma Night
  • 53. ( __bridge_retained cast ) /* ARC */ id object = [[NSObject alloc] init]; void *pointer = (__bridge_retained void *)object; /* not-ARC */ id object = [[NSObject alloc] init]; void *pointer = object; [(id)pointer retain]; • From ARC to Core Foundation • __bridge_retained cast will retain the object just after the assignment is done Pragma Night
  • 54. ( __bridge_transfer cast ) /* ARC */ void *pointer = &bytes; id object = (__bridge_transfer id)pointer; /* not-ARC */ id object = (id)pointer; [object retain]; [(id)pointer release]; • From Core Foundation to ARC • __bridge_transfer cast will release the object just after the assignment is done Pragma Night
  • 55. Toll-Free Bridging /* NSObject.h */ // After using a CFBridgingRetain on an NSObject, the caller // must take responsibility for calling CFRelease at an // appropriate time NS_INLINE CF_RETURNS_RETAINED CFTypeRef CFBridgingRetain(id X) { return (__bridge_retained CFTypeRef)X; } NS_INLINE id CFBridgingRelease(CFTypeRef CF_CONSUMED X) { return (__bridge_transfer id)X; } Pragma Night
  • 56. ARC MACRO // define some LLVM3 macros if the code is // compiled with a different compiler (ie LLVMGCC42) #ifndef __has_ feature #define __has_feature(x) 0 #endif #ifndef __has_extension // Compatibility with pre-3.0 compilers #define __has_extension __has_feature #endif #if __has_feature(objc_arc) && __clang_major__ >= 3 #define ARC_ENABLED 1 #endif Check @ compile-time if ARC is enabled Pragma Night
  • 57. tl;dr What Is ARC ? • Automatic memory management of Obj-C objects • Compiler obeys and enforces existing conventions • Full interoperability with manual retain and release • New runtime features • Weak pointers • Advanced performance optimizations Pragma Night
  • 58. tl;dr What ARC Is NOT ? • No new runtime memory model • No automation for malloc/free, CF, etc. • No garbage collector • No heap scans • No whole app pauses • No non-deterministic releases Pragma Night
  • 59. ARC References • Clang: Automatic Reference Counting documentation • Apple: Transitioning to ARC Release Notes • Apple: Xcode New Features User Guide: Automatic Reference Counting • WWDC 2011 – Session 323 – Introducing Automatic Reference Counting • WWDC 2012 – Session 406/416 – Adopting Automatic Reference Counting • Mike Ash: Friday Q&A about Automatic Reference Counting • Mike Ash: Zeroing weak references without ARC Pragma Night
  • 60. ARC Books Pragma Night
  • 61. NSLog(@”Thank you!”); giuseppe.arici@pragmamark.org Pragma Night