我正在使用仪器的泄漏工具。它说我在 init 方法中有一些泄漏。它表明 NSMutableArray 有泄漏。
我没有看到任何泄漏。
@interface BookSettings : NSObject
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSMutableArray *authors;
@end
- (id)init
{
self = [super init];
if(self)
{
title = [[NSString stringWithString:@""] retain];
authors = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[title release];
[authors release];
[super dealloc];
}
最佳答案
提供的代码没问题,问题出在其他地方,在没有平衡发布的情况下保留了作者。泄漏仅指向创建 ivar 的位置,而不是缺失版本应位于的位置。检查所有保留计数增加的地方。
如果您需要查看某个对象的保留、释放和自动释放发生在何处,请使用工具:
在 instruments 中运行,在 Allocations 中将“Record reference counts”设置为 on(您必须停止记录才能设置该选项)。使问题代码运行,停止记录,搜索感兴趣的 ivar,向下钻取,您将能够看到所有保留、释放和自动释放发生的位置。
认真考虑使用 ARC,没有理由不这样做,ARC 支持回到 iOS 4.x。
顺便说一句:
title = [[NSString stringWithString:@""] retain];
可以写得更紧凑:
title= @"";
关于ios - 我在 init 中有一些泄漏。我看没人漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071868/