问题
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