Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ARC - automatic reference counting

leveton
June 13, 2012
50

ARC - automatic reference counting

What it is and isn't. The main things to consider

leveton

June 13, 2012
Tweet

Transcript

  1. ARC - automatic reference counting ARC is not the same

    as garbage collection ARC does not eliminate retain and release, it just allows the compiler to handle it instead of the developer. It's important to remember that retain and release are still happening. We will see how this can cause you problems
  2. ARC - automatic reference counting Consider this code, which assigns

    a value to an ivar @property (strong, nonatomic) NSString *title; ... @synthesize title = title_; ... title_ = [NSString stringWithFormat:@"Title"]; Without ARC, title_ is under retained in this code. The NSString assigned to it is autoreleased so it will disappear at the end of the run loop, and the next time someone accesses title_, the program will crash.
  3. ARC - automatic reference counting Using ARC, the compiler automatically

    inserts extra code to create the equivalent of this: id oldTitle = title_; title_ = [NSString stringWithFormat:@"Title"]; [title_ retain]; [oldTitle release]; retain and release still happen. Memory is reclaimed faster than with garbage collection. Decisions are made at compile time instead of runtime. ARC-generated memory management is often much faster than hand- coded memory management.
  4. ARC is not garbage collection! <- A retain loop If

    the link between 'External Object' and 'Object A' is broken, a garbage collector would destroy both 'Object A' and 'Object B' With ARC, 'Object A' and 'Object B' are not destroyed because each still has a retain count greater than zero. Lesson - you need to keep track of your strong relationships to avoid these reference loops!
  5. How to Avoid reference loops Anywhere you would have used

    an 'assign' property in the past, use a 'weak' property instead. Most reference loops are caused by delegates, and a delegate property should almost always be weak. weak references have the advantage of automatically being set to nil when the referenced object is destroyed. 'assign' properties can point to freed memory.
  6. Major changes when switching to ARC Don't use retain, release,

    and autorelease. You can just delete these - ARC should do the right thing. if your dealloc only releases ivars, you don't need dealloc. DON'T break naming conventions. For example if you name a method copyRight, ARC looks at the 'copy' part of copyRight and assumes that it returns a +1 retain count object. If copyRight is compiled with ARC and the calling code is compiled with ARC, everything will work. But if the calling code is compiled with ARC and copyRight is not, then ARC will inject an extra release and the program will crash. If copyRight is compiled with ARC, but the calling code is not, then ARC will inject an extra retain, crashing the program.
  7. Major changes when switching to ARC If renaming an incorrect

    method is impossible, you can add the attribute NS_RETURNS_RETAINED or NS_RETURNS_NOT_RETAINED to tell the compiler which memory management rule to use. Don't use NSAutoreleasePool. Rather than creating your own autorelease pools by hand, just wrap any code you want to have its own pool in a @autoreleasepool{} block. @autoreleasepool{} is up to 20x faster than NSAutoreleasePool. Xcode 4 has an ARC Converter! Edit > Refactor > Convert Objective-C to ARC ARC is the greatest advancement in Objective-C since the autorelease pool! Enjoy