-2

Here is my code.

- (void)viewDidLoad{
    [super viewDidLoad];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 240, 280)];
    [view setTag:101];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 220, 260)];
    [view setBackgroundColor:[UIColor redColor]];
    [view addSubview:imgView];
    [self.view addSubview:view];
    [self getimageFromView:view];
    [view release];
    [imgView release];

    [self getimageFromView];
}

-(void)getimageFromView:(UIView *)view{
    for (UIView *view123 in [view subviews]) {
    if ([view123 isKindOfClass:[UIImageView class]]) {
        UIImageView *imgView = (UIImageView *)view123;
        imgView.image = [UIImage imageNamed:@"img.png"];
        NSLog(@"retain cnt 1 = %d",[imgView retainCount]);
    }
    }
}

-(void)getimageFromView{
    for (UIView *view in [self.view subviews]) {
    if (view.tag == 101) {
        for (UIView *view123 in [view subviews]) {
            if ([view123 isKindOfClass:[UIImageView class]]) {
                UIImageView *imgView = (UIImageView *)view123;
                imgView.image = [UIImage imageNamed:@"img.png"];
                NSLog(@"retain cnt 2 = %d",[imgView retainCount]);
            }
        }
    }
    }
}

nslog look like following

retain cnt 1 = 3
retain cnt 2 = 2

Now my questions

1) Why UIImageView's object retain count is displayed like this ? 
2) Is that correct count ?
3) If yes how can i send the release message till it become 0 ?
4) Can I do like this ? Is this proper way ?

for(int i=0;i<[imgView retainCount];i++){
    [imageView release];
}

I have number of views like this and have to do operation on UIImageView as displayed. Also I am getting memory warning and my app getting crash.

Viral Narshana
  • 1,855
  • 2
  • 21
  • 45
  • 5
    [retainCount is useless](http://www.friday.com/bbum/2011/12/18/retaincount-is-useless/) – Ken Thomases Mar 29 '13 at 09:38
  • Try to subclass UIImageView and override retain (don't forget [super retain]) and set breakpoint in this method, to see when it retains. – BergP Mar 29 '13 at 09:40
  • @KenThomases: Are you sure ? I don't think so. Its a very basic and important concept in iOS development – Yogi Mar 29 '13 at 09:45
  • @Yogi Yes he is sure, and I am sure that it is useless to you as a developer. All you should care about is "relative retain count" (meaning balancing your retains with a release) and not "absolute retain count" as `retainCount` returns. – borrrden Mar 29 '13 at 09:50
  • (if you're expecting to see retain cnt 1=2 retain cnt 2=1, it is related to the internal API work) it is reason why we should use memory management rules, we should release only object that we own – BergP Mar 29 '13 at 09:52
  • related: http://stackoverflow.com/questions/4636146/when-to-use-retaincount – Matthias Bauch Mar 29 '13 at 10:25

2 Answers2

1

1) It is displayed like that because that is the retain count.
2) Yes
3) Absolutely not
4) See the answer to 3

borrrden
  • 33,256
  • 8
  • 74
  • 109
1

The documentation for retainCount starts with the statement "Do not use this method." While this is a tad harsh the warning is there because it is very hard to interpret its result.

Answers to your questions:

1) Because that was its value at the time. A value of 3 means that at the instant was returned three objects had expressed ownership interest in the object, and that ownership interest was still outstanding but those objects may have already indicated they wish to cancel that interest by issuing an autorelease - this is one of the reasons retainCount should not be used.

2) The count is always correct at the time it is given, but it can be very hard to interpret.

3) You never, ever attempt to reduce the count to zero, that is completely at odds with the retain/release model and will cause havoc, or worse. The rule is if you create it (using alloc, or methods with create or copy in the names) or retain it then you have an ownership interest and must either hand that ownership to someone else or release/autorelease the object to cancel the ownership interest.

4) See 3, you never get here.

You should read the documentation on the retain/release model and understand ownership. Even under ARC, where retain/release is handled automatically for you, you need to understand ownership.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86