检查保留计数 [英] check retain count

查看:113
本文介绍了检查保留计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做:

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

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

[backgroundImage release];
NSLog(@保留count2:%d,[backgroundImage retainCount]);

我得到了

 保留count1:2 
保留count2:1

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

   - (void)dealloc {

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

2)最后我得到了保留计数 1 对于 backgroundimage 所以没关系,或者它应该 0(零) ??



谢谢.. <根据 Apple docs


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



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



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..

解决方案

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).

这篇关于检查保留计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆