Memory management ARC

12
By: Hossam Ghareeb [email protected]

description

Explaining how memory management works in ARC

Transcript of Memory management ARC

Page 1: Memory management ARC

By: Hossam [email protected]

Page 2: Memory management ARC

What is the ARC?

Convert non-ARC project to an ARC one.

Disable ARC for individual files.

Pointer types (Strong Vs. Weak).

Page 3: Memory management ARC

Upgrading for MRC (Manual Reference Counting)

Page 4: Memory management ARC

Doesn’t change how memory management works, it only changes what you do as a programmer

Preventing you from remembering when to use release, retain, autorelease, but automatically inserts appropriate memory management calls for you at compile time. The compiler also generates appropriate dealloc methods for you

Page 5: Memory management ARC

Xcode provides a tool that automates the mechanical parts of the ARC conversion (such as removing retain and release calls) and helps you to fix issues the migrator can’t handle automatically (choose Edit > Refactor > Convert to Objective-C ARC)

Page 6: Memory management ARC

Go to Target =>Build Phases => Compile Sources in Xcode.

Select desired files

PRESS ENTER

Type -fno-objc-arc

Press Enter or Done

Page 7: Memory management ARC

Strong (similar to retain) By default all instance variables and local variables are strong

pointers.

Strong pointers are “owners” that keep objects alive. (strong = live)

Weak Weak pointers are not owners and do not keep objects alive.

(weak = die).

It is similar to assign but when the object is deallocated, the property value is set to nil instead of remaining as a dangling pointer.

Page 8: Memory management ARC
Page 9: Memory management ARC

Self.companyName = @”Joe’s Diner”

Page 10: Memory management ARC

Self.label.text = @”Catapult City”;

Page 11: Memory management ARC
Page 12: Memory management ARC

If you are making an outlet to a subview of the ViewControllers subview it should be weak. The object exists as long as the top view exists (between viewDidLoad and viewDidUnload). But maybe you want to create another object in your nib file or StoryBoard (a model object). As this object is not under the view hierarchy, you need to make the iboutletstrong.

A weak qualifier is especially used in a parent child object relationship, where the parent has a strong reference to a child object. And the child object a weak reference back to parent otherwise you will end up creating a circular reference(e.g. the parent retains the child and the child retains the parent so neither is ever released).