【发布时间】:2012-04-03 01:32:19
【问题描述】:
我已将问题范围缩小到:
// newImage is passed from elsewhere
NSLog(@"retain count first : %lu", [newImage retainCount]);
img = newImage;
[imgView setImage:newImage];
NSLog(@"retain count next : %lu", [newImage retainCount]);
[imgView setImage:nil];
NSLog(@"retain count finally : %lu", [newImage retainCount]);
以上代码产生:
2012-03-17 21:51:04.833 App[67425:507] retain count first : 1
2012-03-17 21:51:04.833 App[67425:507] retain count next : 2
2012-03-17 21:51:04.834 App[67425:507] retain count finally : 4
如果我注释掉 [imgView setView:nil] 行,代码会生成:
2012-03-17 21:51:52.314 App[67479:507] retain count first : 1
2012-03-17 21:51:52.314 App[67479:507] retain count next : 2
2012-03-17 21:51:52.314 App[67479:507] retain count finally : 2
所以基本上[imgView setImage:nil] 将保留计数增加 2,什么时候应该减少 1?!
【问题讨论】:
-
人们对
retainCount相关的问题很生气,因为它已经出现过很多次了。 -
是的——否则这个问题会很好,但已经有无数个骗子了。
标签: macos cocoa memory-management nsimage nsimageview