Coder Social home page Coder Social logo

fastred / deallocationchecker Goto Github PK

View Code? Open in Web Editor NEW
794.0 794.0 32.0 669 KB

Catch leaking view controllers without opening Instruments.

Home Page: http://holko.pl/2017/06/26/checking-uiviewcontroller-deallocation/

License: MIT License

Ruby 5.78% Swift 87.29% C 0.72% Objective-C 6.21%

deallocationchecker's Issues

Doesn't work when integrated through Carthage

Some time ago (between Xcode 8.3.x and Xcode 9.4) integration through Carthage stopped working. Quick analysis shows that:

  • #if DEBUG scope isn't entered
  • assertions are disabled, so even if we removed #if DEBUG / #endif condition (which is there just for optimization reasons), the assertion still wouldn't fail when needed

I haven't found any justification for this but it seems that at some point Swift Flags and/or Active Compilation Conditions set in the app project stopped being propagated to precompiled frameworks. This looks like a bug fix on Apple's side.

CocoaPods integration is still operational.

Add unit tests

Ideas:

  • add a second internally visible method that also takes an assertion function as a parameter; pass a different function in tests and see if it's called when it should be

Update for Swift 4

You can't use the word "type" for a let/var because there is a function called "type"

let type = type(of:self)

Swizzling for automatic check

Hi, just my 2 cents, because I don‘t want to manually add viewWillDisappear(_:) to all of my view controllers I swizzled the UIViewController method for the check:

import UIKit

private func swizzle(forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
    let originalMethod = class_getInstanceMethod(forClass, originalSelector)
    let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector)
    method_exchangeImplementations(originalMethod, swizzledMethod)
}

extension UIViewController {
    
    class func swizzleForDeallocationCheck() {
        #if DEBUG
            swizzle(forClass: self, originalSelector: #selector(viewWillDisappear(_:)), swizzledSelector: #selector(swizzledViewWillDisappear(animated:)))
        #endif
    }
    
    @objc
    private func swizzledViewWillDisappear(animated: Bool) {
        #if DEBUG
            self.swizzledViewWillDisappear(animated: animated)
            
            //print("viewWillDisappear: class \(type(of: self))")
            
            let delay: TimeInterval = 2.0
            
            // We don't check `isBeingDismissed` simply on this view controller because it's common
            // to wrap a view controller in another view controller (e.g. a stock UINavigationController)
            // and present the wrapping view controller instead.
            if isMovingFromParentViewController || rootParentViewController.isBeingDismissed {
                let type = type(of: self)
                let disappearanceSource: String = isMovingFromParentViewController ? "removed from its parent" : "dismissed"
                
                DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: { [weak self] in
                    assert(self == nil, "\(type) not deallocated after being \(disappearanceSource)")
                })
            }
        #endif
    }
    
    private var rootParentViewController: UIViewController {
        var root = self
        while let parent = root.parent {
            root = parent
        }
        return root
    }

}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
        UIViewController.swizzleForDeallocationCheck()
        // ...
}

UISearchController root?

DeallocationChecker is reporting a leak for me when the view controller I've assigned as searchResultsController in a UISearchController is dismissed. If I understand everything correctly, I don't think this should count as a leak, similar to how UITabBarController is handled, although I'm not 100% sure.

DeallocationChecker.swift:45:17: Method 'objc_dch_checkDeallocation(delay:)' with Objective-C selector 'dch_checkDeallocationAfterDelay:' conflicts with method 'dch_checkDeallocation(afterDelay:)' with the same Objective-C selector

Got this error with the latest version:

Pods/DeallocationChecker/Sources/DeallocationChecker.swift:45:17: Method 'objc_dch_checkDeallocation(delay:)' with Objective-C selector 'dch_checkDeallocationAfterDelay:' conflicts with method 'dch_checkDeallocation(afterDelay:)' with the same Objective-C selector

I'm at swift 3.2, Xcode 9.0.1

Optimize for Swift4

Getting this warning when building with the latest version of Xcode.

The use of Swift 3 @objc inference in Swift 4 mode is deprecated. Please address deprecated @objc inference warnings, test your code with “Use of deprecated Swift 3 @objc inference” logging enabled, and then disable inference by changing the "Swift 3 @objc Inference" build setting to "Default" for the "DeallocationChecker" target.

SwiftPackage not working

Hello,
I think your project is very interesting and I just want to give it a try. Unfortunately I get it in the SWP not imported. I always get the following error message:

The package dependency graph can not be resolved; unable find any available tag for the following requirements:

https://github.com/fastred/DeallocationChecker/ — 2.0.1..<3.0.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.