Model object life cycle introduction

This note explains the life cycle of a model object in an iOS app. It is intended for a new iOS app developer.

.

Object life cycle in iOS 4

iOS 4 uses a reference counting scheme to manage an object’s life cycle.

When an object is created by you (using alloc/init), its retain count is set to one (1). There are two typical situations where the retain count will increase:

  1. If you want to keep (persist) the object, you send it the “retain” message.
  2. If you pass the object along (to another object), its retain count is incremented.

.

When you are finished using an object, you must send it the “release” message.

If a class method creates the object, it returns an “autoreleased” object to you. As above, to increase the retain count, send it the “retain” message, or pass it along to another object.

Unlike above, an “autoreleased” object will have its retain count decremented a short time after block scope ends.

There is an excellent treatment of this topic in Memory Management, an Apple Cocoa Core Competencies topic.

.

Why this note was created

The author was quickly (and carelessly) working on an app, which crashed after attempting to access data. The data was supposed to be persistent, but it (obviously) was not. Here was the scenario:

A model object was defined. It had a normal initializer. It had a property, an array, designed to hold dictionaries of simple strings and integers.

The object was initialized in the app delegate. The array property’s value was set using a class method. The object was then passed on to the root (table) view controller. The root view controller’s UITableView used the object’s array property as a data source.

The table view was built correctly the first time. Subsequent attempts to render the table view led to a crash. Sometimes. Yes, sometimes it would work, and other times it wouldn’t.

The problem? Careless disregard for memory management.

.

The solution

The solution was really simple. The model object’s ivar (array object) needed the “retain” message. You can do this in (at least) two ways:

1) Send the class method the “retain” message, e.g. [[SomeClass returnAnObject] retain]

2) After the class method returns an object to your ivar, send your ivar the “retain” message.

Follow the memory management rules:

If you alloc/init an object, then you must clean up after yourself. Send the object a “release” message when you’re done with it. This rule includes your ivars/properties.

If your object is created by a class method (e.g. arrayWithObjects:), and you want to persist it beyond the current scope, send your object the “retain” message. When you’re finished with your object, send it a “release” message.

.


.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: