MLeaksFinder遇到的问题

转载:http://www.she.vc/article/18-139527-0.html
http://www.jianshu.com/p/fc90383df61f

MLeaksFinder是一个很好的IOS内存泄漏检测工具,能够自动地检测UIViewController和UIView相关的对象,不过最近在团队中使用的时候,有遇到一些问题,总结如下

UITouch持有UIButton的问题
在issue上有讨论
在stroyboard里面使用的问题
在button 的点击事件或者UITableview 的点击Cell的事件中调用self.navigationController popViewControllerAnimated:YES 时就报没有释放
现在框架内的处理是这样的
NSNumber *senderPtr = objc_getAssociatedObject([UIApplication sharedApplication], kLatestSenderKey);
if ([senderPtr isEqualToNumber:@((uintptr_t)self)])
return NO;
简单来说就是看一下是不是跟UIApplication的相关方法打上交道了,如果是的话,忽略该UI控件,不做内存检测(鸵鸟代码吗……
在我们的项目中,确实有遇到类似的场景,表现为UIButton没有及时释放。跟系统版本和机型有关,iPhone6全版本、iPhone5s/iPhone5低于ios9会有问题

UIApplication的方法使用不对
//点击返回按钮,将所有的文本框失焦
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
[self.navigationController popViewControllerAnimated:YES];
解决方法有两个,将button传入,框架将自动忽略

//点击返回按钮,将所有的文本框失焦
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:sender forEvent:nil];
[self.navigationController popViewControllerAnimated:YES];
或者,用更简洁的方式

//点击返回按钮,将所有的文本框失焦
[[UIApplication sharedApplication] resignFirstResponder];
[self.navigationController popViewControllerAnimated:YES];
没有使用delegate
//注册一个文本框失焦的事件
[_phoneTextField addTarget:self action:@selector(editEnd:) forControlEvents:UIControlEventEditingDidEnd];
表现为,当文本框聚焦的时候,点返回按钮,报按钮未释放
解决的方式很简单,用合理的方式去写代码,就能绕过这个坑

_phoneTextField.delegate = self;

  • (void)textFieldDidEndEditing:(UITextField *)textField
    {
    //注册一个文本框失焦的事件
    }
    手势类控件没有释放
    表现为当手势完成,直接让vc进行popViewControllerAnimated的时候,报控件未释放,如果快速点击另外一个按钮的话,则不会报错。
    解决的方法是,在框架的UIView+MemoryLeak.m文件里面,加入下面的代码
    extern const void *const kLatestSenderKey;
  • (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    if(touches.allObjects.count == 1){
    UITouch *touch = [touches anyObject];
    objc_setAssociatedObject([UIApplication sharedApplication], kLatestSenderKey, @((uintptr_t)touch.view), OBJC_ASSOCIATION_RETAIN);
    }
    }
    上述代码的意思是,对于UITouch的最后一次持有的view,不做检测(又是鸵鸟代码…………

键盘没有释放
曾经遇到了一个奇怪的类没有释放
View-ViewController stack: (
RealNameVC,
UIView,
UITableView,
UIKeyboardCandidateInlineFloatingView
)
UIKeyboardCandidateInlineFloatingView是个什么鬼,没见过哦。表现是这样的,在Tableview的一个cell上面,放了一个UITextField,让他聚焦,左上角点按钮返回的时候就报键盘没有释放,手动让UITextField失焦也没有效果。
解决方法很简单,请确保你的模拟器能弹出系统键盘,如果不能,按Command + K即可。切换了以后,记得要重新构建工程!

你可能感兴趣的:(MLeaksFinder遇到的问题)