This document describes how to allocate memory for and initialize Cocoa objects. It also discusses some related issues.
Objective-C Creation and Initialization Methods
How to Allocate and Initialize Objective-C Objects
The class methods alloc, allocWithZone: and new allocate memory for an object and initialize that object’s reference to its class. Every object that inherits directly or indirectly from NSObject is connected to the run-time system through its isa instance variable. isa identifies the object’s class; it references a structure that is compiled from the class definition. Through isa, an object can find whatever information it needs at run-time—such as its place in the inheritance hierarchy, the size and structure of its instance variables, and the location of the method implementations it can perform in response to messages. The object-creation methods also initialize all instance variables to zero (or nil, NULL, or whatever is appropriate for that type).
Note: Objects created and returned by these methods are owned by the receiving object, which is responsible for their disposal.
The initialization methods—instance methods of each class with the name of init or a name beginning with init—initialize individual objects by setting their instance variables to initial values.
In addition, classes frequently define “factory” methods—convenience class methods—that allocate and initialize instances for the receiver.
Invoke alloc or allocWithZone: on a class to create an instance of that class. The alloc method uses the default zone (that is, the zone returned by the NSDefaultMallocZone function) when it allocates memory for an object.
TheClass *newObject = [TheClass alloc]; |
Once you have created an object with alloc, allocWithZone:, or new, you can initialize the object with an init... method. Typically the allocation and initialization messages are combined in one statement.
TheClass *newObject = [[TheClass alloc] init]; |
In some cases, an init method might release the new object and return a substitute. Programs should therefore always use the object returned by init and not necessarily the one returned by alloc or allocWithZone: in subsequent code as illustrated in the following code sample. Note the assignment of self in the test, and use of two pairs of parentheses to avoid compiler warnings with some strict flags.
- (id)init { |
if ((self = [super init])) {// superclass may return nil |
// your initialization code goes here |
} |
return self; |
} |
Subclasses should implement init... to return the new object (self) after it has been successfully initialized; these methods should first invoke super to incorporate the initialization code for the classes they inherit from. If the instance cannot be initialized, they should release the object and return nil.
When one object creates another, it is often a good idea to make sure they are both allocated from the same region of memory. The zone method (declared in the NSObject protocol) can be used for this purpose; it returns the zone where the receiver is located. For example:
id myCompanion = [[TheClass allocWithZone:[self zone]] init]; |
Note that it is your responsibility to release objects (with either release or autorelease) returned by the object-creation methods.
Last updated: 2008-02-08