0

I have a view controller who's views are loaded from a nib file. The nib file contains the main view, which has a scroll view as its subview. Inside view controller's viewDidLoad method, the retain count of the scroll view is 2 (1 for when it was created, and 1 because I retain it). When the view controller's dealloc method is being called, I release the scroll view, but its retain count only decreases to 1, which makes sense since it was 2 at the beginning.

Based on the above scenario, my question is: does the scroll view get fully released after the dealloc method returns, because it is then that the main view is released, forcing all of its subviews (such as the scroll view), to be removed/released? Or, does this release happen at a different point in time?

Thanks!

EDIT: If after I release the scroll view, I set it to nil (while the retain count is still 1), will that scroll view ever get fully released?

Mihai Fonoage
  • 478
  • 2
  • 8
  • 23
  • [When to use retainCount](http://stackoverflow.com/questions/4636146/when-to-use-retaincount). You should really read this. – rckoenes Aug 15 '12 at 13:07

2 Answers2

0

It is retained by the superview it is located in, when that superview goes away it will get another release (and so dealloc'd if the retain count goes to zero). If you're curious you could subclass the UIScrollView and put a NSLog or breakpoint in the dealloc function to be sure. Or, run with the Leaks Instrument and see if it's showing up as a leak.

Bogatyr
  • 18,837
  • 7
  • 56
  • 72
  • Thanks for the confirmation. One last quick question: If after I release the scroll view, I set it to nil (while the retain count is still 1), will that scroll view ever get fully released? – Mihai Fonoage Feb 28 '11 at 21:16
  • You don't set objects to nil, you only set variables to nil. Setting a retained property to nil has the effect of releasing the object once. Settings an "assign" property to nil has no effect on the object's retain count. Variables do not have retain counts, only objects have retain counts. – Bogatyr Mar 01 '11 at 06:36
  • I understand that, but by assigning nil to a property, I'm loosing the reference/pointer to the object it previously was pointing to. I assume that the superview keeps a reference to the scroll view, and that is how any subview gets released in the end. Thanks. – Mihai Fonoage Mar 01 '11 at 12:04
  • If the object in your property has a retain count of 1, and your property is the "retain" type, then doing "self.prop = nil" will release the object, settings its retain count to zero, and triggering the dealloc of the object. – Bogatyr Mar 01 '11 at 12:28
  • Thanks for the reply. I'm talking about having [scrollView release]; scrollView = nil; inside the dealloc method of the view controller. – Mihai Fonoage Mar 01 '11 at 13:33
  • Ah, yes, I understand now. "scrollView = nil" is a simple pointer assignment to your ivar with no side effects. "self.scrollView = nil" is a property assignment which runs the synthesized (or your custom) setter method, which will release the old value if the property is of type "retain". – Bogatyr Mar 01 '11 at 15:30
0

Yes, when you call [super dealloc] at the end.

Felix
  • 35,041
  • 12
  • 93
  • 141
  • Thanks for the confirmation. One last quick question: If after I release the scroll view, I set it to nil (while the retain count is still 1), will that scroll view ever get fully released? – Mihai Fonoage Feb 28 '11 at 21:15