0
votes

I am pushing my UIViewController subclass onto the navigation stack; however, since it is being retained by the navigationController, I 'release' my pointer to it after pushing it onto the stack so that when it is eventually popped the viewController will be dealloc-ed.

However, it's not working, the dealloc method for the viewController is never called. The code looks something like this:

MyViewController *newViewController =
    [self.storyboard instantiateViewControllerWithIdentifier:@"foo"];

[self.navigationController pushViewController:newViewController animated:YES];

newViewController = nil;

Even in the following code, my newViewController is not being dealloc-ed:

MyViewController *newViewController =
    [self.storyboard instantiateViewControllerWithIdentifier:@"foo"];
newViewController = nil;

From what I understand, under the new Automatic Reference Counting (ARC) system, objects are dealloc-ed once nothing points to it. I put a NSLog method in the dealloc method of the viewController that is being created, but it is never called.

What am I missing here?!

Thanks for reading my first post on stack overflow :)

**EDIT:**

I apologise. I tried the second piece of code wrapped in an autorelease pool and it was deallocated. I then tried it without the autorelease pool and it deallocated properly too. I don't know what happened last night.

(2nd edit: and now its stopped working again. fffffffuuuuuuuuuuuu)

1
Since ARC is still under testing, it might still have some bugs.Oscar Gomez
Does instantiateViewControllerWithIdentifier return an autorelease object? i.e. one that has been sent the autorelease message before being returned?freespace
do you have an NSTimer whose target is the viewcontroller? Under ARC you have to invalidate timers as they retain their targets.user156027
I ran into the same problem... Did you figured out how to release the view controller? I am using Xcode5, ARC.Reed

1 Answers

1
votes

In your first example, it would be very odd if the new view controller were deallocated, since you just pushed it onto the navigation controller, and thus the navigation controller has a strong reference to it. The object will only be deallocated when ALL strong references are gone.

In your second example, it's possible that the returned object still has pending releases from autorelease calls that were performed internal to the implementation of instantiateInitialViewController. Thus, until the current autorelease pool is drained, you wouldn't see it disappear. Further, it's possible that the UIStoryboard class caches the returned object as an optimization.

In short, even with ARC you still cannot assume that objects you receive from framework methods will be deallocated immediately when you're done with them, because you cannot guarantee that autorelease was not used in that method's implementation.