views:

110

answers:

1

Instruments says I have leak in this function. I am new to Obj-C so pardon for missing something obvious besides I am not sure if I am doing everything right here.

(void) selectList:sender {
NSMutableString *nibName = @"myController";
MyOwnController *study = [[MyOwnController alloc] initWithNibName:nibName bundle:nil];
study.title = @"Fundamentals";
study.listNameToLoad = @"funds";
[self.navigationController pushViewController:study animated:YES];
[nibName release];
[study.title release];
[study.listNameToLoad release];
[study release];
study = nil;
}

Related questions. Once you do pushViewController should you always do a release on the controller you just pushed on the stack ?

+2  A: 

You should read this first.

In summary, you release only if you create or copy. You have released quite a few variables which you haven't created/copied and is not your responsibility to release. You should hence not release nibName, study.title & study.listNameToLoad.

Again, You should release the 'title' and 'listNameToLoad' properties within the controller's dealloc method.

As for your related question, you can release the controller only if you don't need the reference any more. If you do choose to keep the reference, you need to release the reference later when you don't need it any more.

Deepak
I was trying to appease Instruments but clearly in a wrong way. You mentioned I should not release nibName study.title etc but when you say I should do that in controller's dealloc that needs a bit clarification. Should I say do something like [self.title release] inside controller's dealloc ?
climbon
yes, something similar. You can directly say [title release] within the controller's dealloc but however since 'title' is a property of UIViewController, UIViewController will already have taken care of releasing it within its own dealloc method. However since listNameToLoad is a property of MyOwnController, it should be released within your dealloc method (assumption being that the property was defined as 'retain').
Deepak