【问题标题】:check retain count检查保留计数
【发布时间】:2011-08-20 09:50:41
【问题描述】:

我正在这样做:

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]);

我明白了

retain count1 : 2 
retain count2 : 1

1) 在dealoc 函数中我可以得到如下消息:

- (void)dealloc{

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

并且 2) 最后我得到了 backgroundimage 的保留计数 1 所以没关系,或者应该是 0(zero)

谢谢..

【问题讨论】:

标签: iphone xcode4 dealloc retaincount


【解决方案1】:

根据Apple docs

retainCount 方法不考虑任何待处理的自动释放 发送给接收者的消息。

重要提示:这种方法在调试内存时通常没有任何价值 管理问题。因为任意数量的框架对象可能有 保留一个对象以保存对它的引用,而在 同时自动释放池可能持有任意数量的延迟 在一个对象上发布,你不太可能有用 来自此方法的信息。了解基本规则 必须遵守的内存管理,请阅读《内存管理》 规则”。要诊断内存管理问题,请使用合适的工具: LLVM/Clang 静态分析器通常可以找到内存管理 甚至在您运行程序之前出现问题。对象分配工具 在 Instruments 应用程序中(请参阅 Instruments 用户指南)可以跟踪 对象分配和销毁。 Shark(参见 Shark 用户指南)也 配置文件内存分配(在您的许多其他方面 程序)。

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 2013-02-18
    • 2016-11-21
    • 2010-10-08
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多