V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
newkengsir
V2EX  ›  iOS

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

  •  
  •   newkengsir · 2016-12-08 09:43:59 +08:00 · 2765 次点击
    这是一个创建于 2704 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目介绍地址: 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;
    }
    
    1 条回复    2016-12-08 17:34:58 +08:00
    HuangLibo
        1
    HuangLibo  
       2016-12-08 17:34:58 +08:00
    这个确实很好用,可以自动检测 vc 和 view 的循环引用,推荐!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2375 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:04 · PVG 18:04 · LAX 03:04 · JFK 06:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.