我已经把下面的小例子,其中的Xcode(4.5.2)静态分析显然没有检测到泄漏,为了核实一些意见我已经就这个静态分析:Xcode静态分析器不检测泄漏?
#import <Foundation/Foundation.h>
@interface Foo : NSObject {
NSArray *array;
}
@property (nonatomic, retain) NSArray *array;
- (void)bar;
@end
@implementation Foo
@synthesize array;
- (void)bar
{
// Shouldn't the static analyzer flag this as a leak?
array = [[NSArray alloc] initWithObjects:@"hello", @"world", nil];
}
@end
int main(int argc, const char *argv[])
{
@autoreleasepool {
Foo *foo = [[Foo alloc] init];
[foo bar];
[foo bar];
[foo bar];
[foo release];
}
return 0;
}
如果我没有弄错,重复调用bar
将泄漏NSArray
个实例。 bar
创建一个NSArray
实例,其名称暗示它不会有+1保留计数。先前分配给array
实例变量的实例泄漏,因为它永远不会释放。
然而,真正令我关心的是,我在某处读到ARC基本上使用与静态分析器相同的算法。这是否意味着这个代码也会在ARC下泄漏?或者,即使没有__strong
限定符或相应的(strong)
属性,ARC是否默认将所有实例变量视为强壮?
是的,在ARC下,所有实例变量(和范围内的局部变量)都被视为强变量。 – Mario