Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
luowei opened this issue Jan 13, 2021 · 0 comments
Open

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

luowei opened this issue Jan 13, 2021 · 0 comments

Comments

@luowei
Copy link

luowei commented Jan 13, 2021

我写了一个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];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant