Skip to content

Instantly share code, notes, and snippets.

@HassanSE
Created January 30, 2019 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HassanSE/5a51ff33a5ee77c9cb1fc69b03130223 to your computer and use it in GitHub Desktop.
Save HassanSE/5a51ff33a5ee77c9cb1fc69b03130223 to your computer and use it in GitHub Desktop.
Memory leak catcher
extension UIViewController {
public func dch_checkDeallocation(afterDelay delay: TimeInterval = 5.0) {
let rootParentViewController = dch_rootParentViewController
// 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. in UINavigationController)
// and present the wrapping view controller instead.
if isMovingFromParent || rootParentViewController.isBeingDismissed {
let selfType = type(of: self)
let disappearanceSource: String = isMovingFromParent ? "removed from its parent" : "dismissed"
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: { [weak self] in
assert(self == nil, "\(selfType) not deallocated after being \(disappearanceSource)")
})
}
}
private var dch_rootParentViewController: UIViewController {
var root = self
while let parent = root.parent {
root = parent
}
return root
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment