views:

48

answers:

1

Hi all,

When you dismiss a modal view controller is that view controller object destroyed ?

Also if you represent the same view controller again does it load from fresh - e.g is the "view did load" and "init" method called ?

A: 

Releasing objects is your own responsibility, so you should release the view controller yourself, either after calling presentModalViewController, or sometime later (not recommended)

For example:

MyController *controller = [[MyController alloc] init];

[self presentModalViewController:controller animated:YES];

// "controller" is automatically retained, so you can call release right away    

[controller release];

Calling dismissModalViewController later on will release the retained controller automatically.

Philippe Leybaert
Phillip - thanks. This line of code : [[Mycontroller alloc] initwithNib.... what is the retain count on this object - 1 or 2 ?
Ohnomycoco
It will be one, but it doesn't matter. Think of object ownership, not retain count. You're always responsible for releasing anything that you allocated or retained. Always.
Philippe Leybaert
In the above example, why is "controller" automatically retained ? I am confused how you can alloc controller in line one and then release straight away - doesnt that leave it with a retain count of 0 which would flush it ? Thank you.
Ohnomycoco
the method "presentModalViewController" will hold on to the controller by retaining it. Again, think of ownership. If you hand an object to another method/class, that method should make sure it keeps the object around. You shoudn't actually care about that, as long as you release what you allocated
Philippe Leybaert
Thanks. I have an odd problem which I can't figure out. I am using the following structure. A method (as the one you list above) in main menu presents a viewcontroller (via a sinlgeton method in the viewcontroller). That viewcontroller then initiates a game object (subclass of NSOBject) and assigns a view to this object. When the game is over the game object then tells the view controller to dismiss itself. The odd thing is that the first two times the game ends this works and returns to the main menu, however on the third time the app crashes and I get an error in "Main".
Ohnomycoco
If I delete the "super dealloc" command in the dealloc method in the view controller this solves the problem. Also if I remove the line [controller release] in your example above this also solves the problem. I am not sure what is happening.
Ohnomycoco
You must have a alloc/retain vs release/autorelease problem. But without seeing the full code, it's hard to tell what's going on. (and showing the full code is probably not a good idea on stackoverflow)
Philippe Leybaert
Thanks Phillippe - can you give me some general things to check - e.g is it likely to be the viewcontroller itself or an object in the viewcontroller ?
Ohnomycoco