1

In my mind, myViewController should be deallocated around the time that I pop back to the root view controller with the following code, but I never see the deallocation message getting NSLogged.

If this should work, then what kind of problem can I look for in the myViewController's class that might cause it to get deallocated when I popToRootViewController?

Thanks.

The following gets called in my tableView:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

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

[vc release];
}

UPDATE:

This code was perfect, but it was some bad memory management in my custom view controllers that caused neither to be released. I had some retained properties that should have been assign instead (or at least, that's the way I solved it). See comments for specifics.

JaySmith02
  • 11
  • 1
  • 4
  • When you are on different view controller on navigation controller, if you are on iOS Simulator Hardware -> Simulate Memory Warning and then test it. Navigation controller will dealloc your view controller (if its not root view controller) if your application receives memory warning. – 0x8badf00d Dec 27 '11 at 16:53
  • 1
    there is no problem in the code you showed. Need to see the code in MyViewController - you might have retained the controller somewhere in its own methods. – Saurabh Passolia Dec 27 '11 at 16:54
  • Called Simulate Memory Warning, still no deallocation. I guess I should check out the properties of myViewController next.. – JaySmith02 Dec 27 '11 at 16:58
  • Interesting... so basically one of the properties of myViewController is @property (nonatomic, retain) ScrollViewViewController *scrollViewViewController; This is the primary view inside of myViewController where all of the action happens. When I switched that to assign instead of retain, myViewController gets deallocated when I popToRootViewController, but then I get a crash. – JaySmith02 Dec 27 '11 at 17:04
  • My hunch is that you have more then one myViewController maybe from the `[self.navigationControll pushViewController:vc...]`, I have a feeling the dealloc message will only be sent when the reference count is zero. Not sure if this is anywhere near correct so this is not an answer :-) will be interesting to see what others say – T I Dec 27 '11 at 17:05
  • I'm no longer getting a crash when it gets deallocated because I took out the release message that previously took care of the retain property in the dealloc method. Now myViewController gets completely deallocated, but scrollViewViewController is never getting deallocated with it. – JaySmith02 Dec 27 '11 at 17:09
  • @JaySmith02 it sounds like you have a retain cycle between MyViewController and ScrollViewViewController. You've "fixed" that (in a bad way), but you should fix it correctly. If you show those two controllers and relationship(s) between them, I'm sure someone can help. – Firoze Lafeer Dec 27 '11 at 17:21
  • Ok, so you're right... I now have both MyViewController and ScrollViewViewController getting deallocated together when I popToRootViewController, exactly what I want. But... you say I've done that in a bad way (i.e. I simply changed properties from retain to assign; By the way, ScrollViewViewController wasn't getting deallocated because it also had a retained property called OwnerView that kept a reference to MyViewController. Once I made that property assign as well, both got deallocated.) What would be the correct way of solving this problem? (I'll give code in the next response) – JaySmith02 Dec 27 '11 at 17:36
  • In MyViewController's viewDidLoad method, I'm calling self.scrollViewViewController_iPhone = [[ScrollViewViewController_iPhone alloc] initWithNibName:@"ScrollViewViewController_iPhone" bundle:nil]; in order to allocate our "scroll view subview". Then, in a method called SetUpScrollView, I add it to a UIScrollView instance instantiated via IB ( a retained IBOutlet property of MyCustomViewController) with the following code: [self.scrollView addSubview:[self.scrollViewViewController_iPhone view]]; – JaySmith02 Dec 27 '11 at 17:41
  • @JaySmith02 it depends on how many and which properties you changed to assign. I said "bad way" earlier, because earlier you were leaking the ScrollViewViewController. If you have fixed that issue since then, then it's quite possible you are doing it correctly now. Please edit the additional code into your original question so people can read it. – Firoze Lafeer Dec 27 '11 at 17:42
  • Also, in that same method, I am doing the following: [self.scrollViewViewController_iPhone setOwnerView:self]; in order for scrollViewViewController to be able to access a few properties of myViewController. – JaySmith02 Dec 27 '11 at 17:42
  • Yeah, the only properties I changed to assign were (nonatomic, retain) ScrollViewViewController_iPhone *scrollViewViewController_iPhone and (nonatomic, retain) MyViewController *ownerView. I also got rid of the autoreleases when I allocated and set the retained properties, and kept the release and nil in dealloc method. Everything seems to be working fine now, thanks. – JaySmith02 Dec 27 '11 at 17:47
  • @JaySmith02 you shouldn't have needed to change both. I would just change the property 'ownerView'. You only need/want to break one link of a retain cycle. If that doesn't work, then you likely have some other memory issues as well. – Firoze Lafeer Dec 27 '11 at 17:56
  • Yup, I changed the ScrollViewViewController property back to retain, added back the autorelease at allocation, and left ownerView as assign. Still deallocating both at the right time, thanks! – JaySmith02 Dec 27 '11 at 18:51

2 Answers2

0

@JaySmith02 is right

it was some bad memory management in my custom view controllers that caused neither to be released. I had some retained properties that should have been assign instead (or at least, that's the way I solved it)

In my case the culprit was

@property (nonatomic, retain) id<TTSlidingPagesDataSource> dataSource;

From my viewController when I wrote

slidingPages.dataSource = self

I guess the dataSource retained my viewController and made a circular retention. The 'dealloc' of my viewController was never getting called.

Note: Under ARC dealloc does get called. Difference is you cannot call [super dealloc]

The solution:

@property (nonatomic, assign) id<TTSlidingPagesDataSource> dataSource;
Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107
0

When you use poptoviewcontroller then dealloc method will call for the topmost view controller in navigation controller. You can put a breakpoint in dealloc method of your current view controller and when you called popviewcontroller then your dealloc method gets called and release all the stuff/varaibles you have created in your view controller.

Sandeep Dhama
  • 614
  • 1
  • 6
  • 20