check retain count

折月煮酒 提交于 2019-11-30 09:08:20

问题


I am doing this :

UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mainback.jpg"]];

[self.view addSubview:backgroundImage];
NSLog(@" retain count1 : %d " , [backgroundImage retainCount]);
[self.view sendSubviewToBack:backgroundImage];

[backgroundImage release];
NSLog(@" retain count2 : %d " , [backgroundImage retainCount]);

I got

retain count1 : 2 
retain count2 : 1

1 ) in dealoc function can i get message like :

- (void)dealloc{

NSLog(@" retain count2 : %d " , [backgroundImage retainCount]);
[super dealloc];
}

And 2) at last i got retain count 1 for backgroundimage so it is ok or it should be 0(zero)??

Thanks..


回答1:


According to the Apple docs,

The retainCount method does not account for any pending autorelease messages sent to the receiver.

Important: This method is typically 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. To understand the fundamental rules of memory management that you must abide by, read “Memory Management Rules”. To diagnose memory management problems, use a suitable tool: The LLVM/Clang Static analyzer can typically find memory management problems even before you run your program. The Object Alloc instrument in the Instruments application (see Instruments User Guide) can track object allocation and destruction. Shark (see Shark User Guide) also profiles memory allocations (amongst numerous other aspects of your program).



来源:https://stackoverflow.com/questions/7131014/check-retain-count

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