views:

22

answers:

2

Hi,

recently I joined two Xcode projects together. To get this thing to work, I had to alloc and initialize my view controller.

self.myViewController = [[MyViewController alloc] init];

But why? In the other project I have the same code. The sole difference is hierarchy of the different views. I added a new view to the top (beginning). So the calling View Controller is not the first view on the stack anymore.

I'm pushing my view in this way on the stack:

[[self navigationController] pushViewController:myViewController animated:YES];

In my NIBs I have added a View Controller object with IB and connected the Outlets.

And I have a memory management question too: If I have a property like myViewController, do I have to release it? The "normal" release is done in the dealloc method. But do I have to use an additional release because of the alloc? I don't think so, but I ask you anyway.

A: 

I found it out: In IB I had to set the nib name on my view controller object. So the alloc and initialization is done by IB?

testing
A: 

I would need to see more code to answer why you had to alloc your view controller, but I'd say that you always alloc them manually (at least in my experience).

As for the memory management question, if your property is declared as a retain property (@property(retain) UIViewController *myViewController), you are indeed leaking memory, since the retain count after alloc will be 1, and after the retain done by your accessor will be 2. Hence, if you release it only once, you'll end up with a leak.

I usually do this instead:

self.myViewController = [[[MyViewController alloc] init] autorelease];
pgb
Thanks for your answer! There is not anymore code behind. I'm doing it completely with IB because I was confused of using arrayWithObject:viewControllers/pushViewController/addSubView/initWithNibName ... So either releasing it two times or using autorelease?
testing