小编Sun*_*man的帖子

使用xCode 4.3在iOS中出现泄漏检测问题

我对xCode 4.3内存泄漏仪器有一个非常奇怪的问题......基本上它在以下情况下不起作用......

  1. 项目创建时没有ARC支持.
  2. 创建一个继承UIView的简单类
  3. 使用"按钮"来创建此类的实例并"泄漏"它......漏洞将不会被泄漏仪器捕获

所以这里是PROBLEMATIC类的代码

@interface LeakTestView :  UIView
- (id)initWithFrame:(CGRect)frame;
@end

@implementation LeakTestView
- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"initWithFrame called");
    self = [super initWithFrame:frame];
    if (self) {

        // Initialization code
    }
    return self;
}
@end
Run Code Online (Sandbox Code Playgroud)

现在我创造了泄漏......

- (IBAction)leak:(id)sender {
    LeakTestView* leak=[[LeakTestView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSLog(@"class is %@", [leak class]);
}
Run Code Online (Sandbox Code Playgroud)

所以问题是这个泄漏不会被发现......

如果我将基类更改为NSObject而不是initWithFrame重写init(请参阅下面的内容),那么将检测到泄漏....

所以这里是将被检测到的泄漏代码

@interface LeakTestView : NSObject
- (id) init;
@end


@implementation LeakTestView

- (id) init {
    NSLog(@"init called");
    self = [super init];
    if (self) {

    }
    return self; …
Run Code Online (Sandbox Code Playgroud)

xcode memory-leaks ios xcode-instruments automatic-ref-counting

7
推荐指数
1
解决办法
706
查看次数