关于iOS里IBOutlet对象的内存管理

猪大肾
  • 64

table是和xib绑定的对象,在vc释放时打印_table的引用数居然是2,哪位高手能解答一下

环境:xcode5 sdk7

@interface testViewController : UIViewController {

}

@property (retain, nonatomic) IBOutlet UITableView *table;
@end 
@implementation testViewController
- (void)dealloc {
    [_table release];
    [super dealloc];
    NSLog(@"%d",[_table retainCount]);
}
@end
回复
阅读 4.5k
1 个回答

Nib Files

你需要了解nib以及outlet的工作方式:当加载nib文件的时候, table会被初始化,这时候它的retain count是1, 然后建立nib文件里对象的连接,这时候会调用到你定义的属性的set方法, 因为你定义成了retain, 所以这时候table的retain count变成了2, 属性的getter/setter会用到autorelease,这时候就会变成3!

推荐IBoutlet的属性定义成assign(weak)就可以了!

其实你不需要在意retain count:“This method is 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.”

宣传栏