看别人博客内容中有写关于 [NSNotificationCenter defaultCenter]addObserverForName...存在内存泄露的可能. 自己写一个demo没有检测出来.
@interface TestViewController ()
@property (copy, nonatomic) NSString* aStr;
@end
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSOperationQueue* queue = [[NSOperationQueue alloc]init];
[[NSNotificationCenter defaultCenter]addObserverForName:UIKeyboardDidShowNotification object:nil queue:queue usingBlock:^(NSNotification * _Nonnull note) {
self.aStr = @"JJJ";
}];
FBRetainCycleDetector *detector = [FBRetainCycleDetector new];
[detector addCandidate:self];
NSSet *retainCycles = [detector findRetainCycles];
NSLog(@"%@", retainCycles); //利用FBRetainCycleDetector 却检测不出来内存泄露.但是 dealloc方法在pop的时候没有执行,哪位朋友知道这是为什么吗?
}
- (void)dealloc{
NSLog(@"dealloc method excute!");
}
利用FBRetainCycleDetector 却检测不出来内存泄露是因为根本就没有循环引用嘛。
[NSNotificationCenter defaultCenter]
强引用usingBlock
,usingBlock
中强引用self
。self
在被 pop 之后并不会被销毁是因为被[NSNotificationCenter defaultCenter]
间接强引用。