Memory management ARC

Post on 04-Jul-2015

200 views 0 download

description

Explaining how memory management works in ARC

Transcript of Memory management ARC

By: Hossam Ghareebhossam.ghareb@gmail.com

What is the ARC?

Convert non-ARC project to an ARC one.

Disable ARC for individual files.

Pointer types (Strong Vs. Weak).

Upgrading for MRC (Manual Reference Counting)

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

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)

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

Select desired files

PRESS ENTER

Type -fno-objc-arc

Press Enter or Done

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.

Self.companyName = @”Joe’s Diner”

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

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).