For C++ programmers the first surprise waiting for them in Objective-C is that in a derived class they must explicitly call the init and dealloc methods of the superclass (roughly the equivalent of a constructor and destructor).
By following the idioms below you can avoid a lot of headaches.
[sourcecode title=”// init and dealloc idioms” language=”objc”]
if(self = [super init])
// how to handle failure
memberVariable = [[SomeOtherClass alloc] init];
if(memberVariable == nil)
// Release anything you alloced or retained…
// … then dealloc your super class
When accessing member variables in
dealloc you show access them directly, not through accessors, to avoid side-effects.