ARC
Automatic Reference Counting (ARC) is 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, the object graphs, and the relationships between objectsin your application
ARC works by adding code at compile time to ensure that objects live as long as necessary, but no longer.
Conceptually, it follows the same memory management conventions as manual reference counting by adding the appropriate memory management
calls for you.
- (void)contrived {
Person *aPerson = [[Person alloc] init];
[aPerson setFirstName:@"William"];
[aPerson setLastName:@"Dudney"];
[aPerson setYearOfBirth:[[NSNumber alloc] initWithInteger:2011]];
NSLog(@"aPerson: %@", aPerson);
}
ARC takes care of memory management so that neither the Person nor the NSNumber objects are leaked.
->You cannot explicitly invoke dealloc, or implement or invoke retain, release, retainCount, or
autorelease.
The prohibition extends to using @selector(retain), @selector(release), and so on.
->You cannot use NSAllocateObject or NSDeallocateObject.
->You cannot use object pointers in C structures.
->● You cannot use NSAutoreleasePool objects.
ARC provides @autoreleasepool blocks instead. These have an advantage of being more efficient than
NSAutoreleasePool.
->● You cannot use memory zones.
There is no need to use NSZone any more—they are ignored by the modern Objective-C runtime anyway.
->You cannot give an accessor a name that begins with new. This in turn means that you can’t, for example,
declare a property whose name begins with new unless you specify a different getter:
->The keywords weak and strong are introduced as new declared property attributes, asshown in the following
examples.
->// The following declaration is a synonym for: @property(retain) MyClass *myObject;
@property(strong) MyClass *myObject;
->// The following declaration is similar to "@property(assign) MyClass *myObject;"
// except that if the MyClass instance is deallocated,
// the property value is set to nil instead of remaining as a dangling pointer.
@property(weak) MyClass *myObject;
->Under ARC, strong is the default for object types
VariableQualifiers
__strong
__weak
__unsafe_unretained
__autoreleasing
● __strong is the default. An object remains “alive” as long as there is a strong pointer to it.
● __weak specifies a reference that does not keep the referenced object alive. A weak reference is set to
nil when there are no strong references to the object.
Transitioning to ARC Release Notes
ARC Overview
● __unsafe_unretained specifies a reference that does not keep the referenced object alive and is not
set to nil when there are no strong references to the object. If the object it references is deallocated, the
pointer is left dangling.
● __autoreleasing is used to denote argumentsthat are passed by reference (id *) and are autoreleased
on return.
Eg:
NSString * __weak string = [[NSString alloc] initWithFormat:@"First Name: %@",
[self firstName]];
NSLog(@"string: %@", string);
Although string is used after the initial assignment, there is no other strong reference to the string object
at the time of assignment; it is therefore immediately deallocated. The log statement shows that string has
a null value. (The compiler provides a warning in this situation.)
Eg 2:
NSError *error;
BOOL OK = [myObject performOperationWithError:&error];
if (!OK) {
// Report the error.
// ...
However, the error declaration is implicitly:
NSError * __strong e;
and the method declaration would typically be:
-(BOOL)performOperationWithError:(NSError * __autoreleasing *)error;
The compiler therefore rewrites the code:
NSError * __strong error;
NSError * __autoreleasing tmp = error;
BOOL OK = [myObject performOperationWithError:&tmp];
error = tmp;
if (!OK) {
// Report the error.
// ...
The mismatch between the local variable declaration (__strong) and the parameter (__autoreleasing)
causesthe compiler to create the temporary variable. You can get the original pointer by declaring the parameter
id __strong * when you take the address of a __strong variable. Alternatively you can declare the variable
as __autoreleasing.
Patterns for Managing Outlets Become Consistent Across Platforms
The patterns for declaring outlets in iOS and OS X change with ARC and become consistent across both
platforms. The pattern you should typically adopt is: outletsshould be weak, except for those from File’s Owner
to top-level objects in a nib file (or a storyboard scene) which should be strong
Stack Variables Are Initialized with nil
Using ARC,strong, weak, and autoreleasing stack variables are now implicitly initialized with nil. For example:
- (void)myMethod {
NSString *name;
NSLog(@"name: %@", name);
}
will log null for the value of name rather than perhaps crashing.
No comments:
Post a Comment