1

I have a UIView that when I initialize it has already retain count 2 and I do not understand why, as a result I can not remove it with removefromsuperview

ViewController.h

  @property (nonatomic, retain)FinalAlgView * drawView;

ViewController.m

  self.drawView =[[FinalAlgView alloc]init];

 NSLog(@"the retain count 1 of drawView is %d", [self.drawView retainCount]);
 //the retain count 1 of drawView is 2

 [self.bookReader addSubview:self.drawView];

 NSLog(@"the retain count 2 of drawView is %d", [self.drawView retainCount]);
 //the retain count 2 of drawView is 3

 [self.drawView release];

 NSLog(@"the retain count 3 of drawView is %d", [self.drawView retainCount]);
 //the retain count 3 of drawView is 2

 [UIView animateWithDuration:0.2
                 animations:^{self.drawView.alpha = 0.0;}
                 completion:^(BOOL finished){ [self.drawView removeFromSuperview];
                 }]; 
 //do not remove

I not use ARC

Ortensia C.
  • 4,666
  • 11
  • 43
  • 70
  • There is only one answer to your question: http://stackoverflow.com/questions/4636146/when-to-use-retaincount/4636477#4636477 – rckoenes Oct 02 '13 at 08:02

2 Answers2

4

You cannot count on retainCountyou will get confusing result, and better don't use it at all.

From Apple:

... it is very unlikely that you can get useful information from this method.

Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
0

As null said, you can't rely on retainCount. Assuming that you're using ARC, your code is actually compiling to something like this:

FinalAlgView *dv = [[FinalAlgView alloc] init]; // Starts with retainCount of 1
self.drawView = dv; // Increments the retainCount

 NSLog(@"the retain count 1 of drawView is %d", [self.drawView retainCount]);
 //the retain count 1 of drawView is 2

...
// do not remove
...
[dv release];

If you aren't using ARC, then you need to change your first line of code to this:

self.drawView =[[[FinalAlgView alloc]init]autorelease];

The retainCount will still start at 2 until the autorelease pool is drained at the end of the runloop.

bbum
  • 162,346
  • 23
  • 271
  • 359
Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
  • also used addSubview that increments it to 3, then how do I lower it to use removeFromSuperView? – Ortensia C. Oct 02 '13 at 08:14
  • When you call `removeFromSuperview` the retainCount decrements. – Guy Kogus Oct 02 '13 at 08:14
  • but removeFrmSuperview not remove the view – Ortensia C. Oct 02 '13 at 08:17
  • `UIView`s have this chained effect that, when one view is released, it removes all its subviews. If there is nothing retaining those subviews, they in turn get released and the process continues until all views are released. So long as you release your reference to the view (`self.drawView = nil`) your view will be dealloc'd. – Guy Kogus Oct 02 '13 at 08:18
  • I don't understand you. – Guy Kogus Oct 02 '13 at 08:19
  • Absolute retain count is meaningless. – bbum Oct 02 '13 at 13:23
  • @GuyKogus Because absolute retain counts are useless and, though the first line points that out sort of, the rest of it makes claims about the absolute retain count. While those claims are likely correct, it all falls apart as soon as the view is stuck in the view hierarchy because the UIKit may retain/autorelease the view multiple times over the view's lifespan, including -- potentially -- from multiple threads – bbum Oct 02 '13 at 15:05
  • I don't see what any of that has to do with my answer and it being downvoted. I was trying to inform the questioner a bit more about retain counts. I have found that newer iOS developers don't understand it fully since they never had to deal with manually calling retain/release/autorelease. Thank you for undoing the downvote nevertheless. – Guy Kogus Oct 02 '13 at 15:18