iOS about retainCount

微笑、不失礼 提交于 2019-12-11 10:23:22

问题


Now I have a ClassA : NSObject, and then in the viewcontroller viewDidLoad, look at the under code:

- (void)viewDidLoad {
    ClassA *a = [[ClassA alloc] init];
    NSLog(@"a retainCount = %d", [a retainCount]);
    ClassA *b = a;
    NSLog(@"a retainCount = %d b retainCount= %d ", [a retainCount],[b retainCount]);
    [a release];
    NSLog(@"a retainCount = %d b retainCount= %d", [a retainCount],[b retainCount]);
    [super viewDidLoad];
}

The console output looks like:

2012-11-02 14:43:35.437 RetainCountTest[1043:207] a retainCount = 1
2012-11-02 14:43:35.439 RetainCountTest[1043:207] a retainCount = 1 b retainCount= 1  
2012-11-02 14:43:35.439 RetainCountTest[1043:207] a retainCount = 1 b retainCount= 1

I don't understand that when I call [a release], why the [a retainCount]== 1


回答1:


You should never pay attention to retainCount's. They are at best confusing, and at worst misleading. One should just make sure that they follow memory management rules of retain/release correctly, and forget about retainCounts.

From documentation..

This method is of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

EDIT : Suggested Reading

When to use -retainCount?

EDIT : After seeing OP's comment

From Cocoa Core Memory Management rules

When you create or copy an object, its retain count is 1. Thereafter other objects may express an ownership interest in your object, which increments its retain count. The owners of an object may also relinquish their ownership interest in it, which decrements the retain count. When the retain count becomes zero, the object is deallocated (destroyed).

If one read this, he/she might think, Oh retainCount is godsent, and I can see the complete alloc/retain/release cycle of an object just using an NSLog statement. But it doesn't actually works that way. You cannot say, you have sole ownership of object you create. That object might be retained by any other framework object. And by releasing you are just relinquishing your ownership. The object will get released only after all other objects remove their reference.

I don't know why it remains in public API.




回答2:


That's because "retainCount is useless".

To add some context: You should expect undefined behavior when messaging an object which is 'dead'.




回答3:


When you call [a release], you're not holding on to a anymore, so a might get deallocated. It's probably the case here since its ownership is mot shared.

Any further message to a has an undefined return value: another object might have reused a's memory slot.

So the return value you print is essentially random. It might have crashed your app or printed 1000...



来源:https://stackoverflow.com/questions/13190640/ios-about-retaincount

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!