I am struggling in iOS (4) with allocating objects in one scope and releasing it in another scope. I am using properties as pointers to the objects. The object is allocated and initialized in one instance class method and I release it in the class’ dealloc method. The properties are declared with retain. Besides having problems with using properties likes this I also find it cumbersome to alloc and initialize the object and set the property.
NSObject *object = [[NSObject alloc] init];
Self.myProperty = object;
[object release];
I tried
self.myObject = [[NSObject alloc] init];
However that gave me a memory leak.
My question: Do I have to make this temporary object or is there a more elegant way of doing this?
A followup question: Do I have to both set the property to nil and release the autogenerated ivar?
[self setMyProperty:nil], [myProperty release];
When XCode 4 generates property stubs for you it puts [self setMyProperty:nil] in viewDidUnload and [_myProperty release] in dealloc.