[iOS 源代码分享] MLeaksFinder 可帮助您在开发时查找您的 iOS 应用程序中的内存泄漏

2016-12-08 09:43:59 +08:00
 newkengsir

项目介绍地址: http://www.code4app.com/forum.php?mod=viewthread&tid=11844&extra=page%3D4

##MLeaksFinder

MLeaksFinder helps you find memory leaks in your iOS apps at develop time. It can automatically find leaks in UIView and UIViewController objects, present an alert with the leaked object in its View-ViewController stack when leaks happening. More over, it can try to find a retain cycle for the leaked object using FBRetainCycleDetector. Besides finding leaks in UIView and UIViewController objects, developers can extend it to find leaks in other kinds of objects.

MLeaksFinder 可帮助您在开发时查找您的 iOS 应用程序中的内存泄漏。 它可以自动查找 UIView 和 UIViewController 对象中的泄漏,当泄漏发生时,在其 View-ViewController 堆栈中显示泄漏对象的警报。 更多,它可以尝试使用 FBRetainCycleDetector 找到泄漏对象的保留周期。 除了在 UIView 和 UIViewController 对象中查找泄漏,开发人员可以扩展它以查找其他类型对象中的泄漏。

Communication

QQ group: 482121244

Installation

pod 'MLeaksFinder'

MLeaksFinder comes into effect after pod install, there is no need to add any code nor to import any header file.

MLeaksFinder 在`pod install'之后生效,不需要添加任何代码或导入任何头文件。

Usage

MLeaksFinder can automatically find leaks in UIView and UIViewController objects. When leaks happening, it will present an alert with the leaked object in its View-ViewController stack.

MLeaksFinder 可以自动查找 UIView 和 UIViewController 对象中的泄漏。 当泄漏发生时,它将在其 View-ViewController 堆栈中提供泄漏对象的警报。

Memory Leak
(
    MyTableViewController,
    UITableView,
    UITableViewWrapperView,
    MyTableViewCell
)

For the above example, we are sure that objects of MyTableViewController, UITableView, UITableViewWrapperView are deallocated successfully, but not the objects of MyTableViewCell.

MLeaksFinder can also try to find a retain cycle for the leaked object using FBRetainCycleDetector.

对于上面的例子,我们确信“ MyTableViewController ”,“ UITableView ”,“ UITableViewWrapperView ”的对象被成功解除分配,但不是“ MyTableViewCell ”的对象。

MLeaksFinder 也可以尝试使用 FBRetainCycleDetector 查找泄漏对象的保留循环。

Retain Cycle
(
    "-> MyTableViewCell ",
    "-> _callback -> __NSMallocBlock__ "
)

With the output, we know that the object of MyTableViewCell has a __strong instance variable named _callback, which is of type __NSMallocBlock__. And _callback also has a __strong reference back to MyTableViewCell.

使用输出,我们知道MyTableViewCell的对象有一个名为_callback__strong实例变量,它的类型为__NSMallocBlock__。 和_callback也有一个__strong引用返回MyTableViewCell

Mute Assertion

If your class is designed as singleton or for some reason objects of your class should not be dealloced, override - (BOOL)willDealloc in your class by returning NO.

如果你的类被设计为单例,或者由于某些原因你的类的对象不应该被释放,在你的类中重写- ( BOOL ) willDealloc,返回 NO 。

- (BOOL)willDealloc {
    return NO;
}

Find Leaks in Other Objects

MLeaksFinder finds leaks in UIView and UIViewController objects by default. However, you can extend it to find leaks in the whole object graph rooted at a UIViewController object.

默认情况下, MLeaksFinder 在 UIView 和 UIViewController 对象中找到泄漏。 但是,您可以扩展它以在基于 UIViewController 对象的整个对象图中查找泄漏。

- (BOOL)willDealloc {
    if (![super willDealloc]) {
        return NO;
    }
    
    MLCheck(self.viewModel);
    return YES;
}
2798 次点击
所在节点    iOS
1 条回复
HuangLibo
2016-12-08 17:34:58 +08:00
这个确实很好用,可以自动检测 vc 和 view 的循环引用,推荐!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/326095

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX