- 先让我们来看看正确的姿势, 一定要在想要释放内存前把持有的对象释放掉, 千万不要在dealloc 里面写, 根本没有机会走到dealloc, 这个跟NSTime 类类似的道理,[self.timer invalidate]; 要提前处理掉
@property (nonatomic, strong) id observer;
- (void) viewDidLoad {
self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSLog("hello");
}];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if (self.observer) {
[[NSNotificationCenter defaultCenter] removeObserver:self.observer];
self.observer = nil;
}
}
- (void)dealloc
{
NSLog(@"MTC_dealloc OK");
}
下面这个兄弟写错了 , 永远不会释放掉这个UIViewControl了……
http://www.jianshu.com/p/1788d15c570b
本文介绍了一个重要的iOS开发技巧——正确管理内存。强调了在需要释放内存之前解除对象引用的重要性,并提供了一个具体的例子来说明如何在视图消失时取消观察者以避免内存泄漏。
592

被折叠的 条评论
为什么被折叠?



