OOMDetector icon indicating copy to clipboard operation
OOMDetector copied to clipboard

为什么无法检测到OC对象的内存泄漏?

Open luowei opened this issue 4 years ago • 0 comments

我写了一个ViewController测试以下几种方式引发的内存漏泄,但是发现日志并没有检测结果,这是什么原因?

这个ViewController 是这样写的:

@interface TableView : NSObject
@property(nonatomic) Byte *space;
@property(nonatomic, copy) void (^block)();
-(void)sayHello;
@end

#define kTenMB  1048576 * 10
@implementation TableView
- (instancetype)init {
    self = [super init];
    if (self) {
        //分配 10MB 内存
        self.space = malloc(kTenMB);
        memset(self.space, 0, kTenMB);
    }
    return self;
}
-(void)sayHello {
    NSLog(@"Hello!");
}
- (void)dealloc {
    NSLog(@"=== %s",__func__);
    free(self.space);
}
@end


@interface AViewController ()
@property(nonatomic, copy) void (^block)();
@property(nonatomic, strong) TableView *tableView;

@end

@implementation AViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"A";
    self.view.backgroundColor = UIColor.greenColor;
    
// ---- Block ,self leak ----
   self.block = ^{
//        [self test];
       NSLog(@"== title:%@",self.title);
   };
   self.block();

   // ---- tableView leak ----
   TableView *tableView = [TableView new];
   tableView.block = ^{
       [tableView sayHello];
   };
    
}

- (void)dealloc {
    NSLog(@"==== %s %@",__func__,self);

}

@end

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 也做了以下配置:

[[OOMDetector getInstance] setupWithDefaultConfig];
// 开启内存泄漏监控
[[OOMDetector getInstance] setupLeakChecker];

luowei avatar Jan 13 '21 18:01 luowei