diff --git a/MLeaksFinder/UINavigationController+MemoryLeak.m b/MLeaksFinder/UINavigationController+MemoryLeak.m index 94e2add..099f1f1 100644 --- a/MLeaksFinder/UINavigationController+MemoryLeak.m +++ b/MLeaksFinder/UINavigationController+MemoryLeak.m @@ -23,6 +23,7 @@ + (void)load { [self swizzleSEL:@selector(popViewControllerAnimated:) withSEL:@selector(swizzled_popViewControllerAnimated:)]; [self swizzleSEL:@selector(popToViewController:animated:) withSEL:@selector(swizzled_popToViewController:animated:)]; [self swizzleSEL:@selector(popToRootViewControllerAnimated:) withSEL:@selector(swizzled_popToRootViewControllerAnimated:)]; + [self swizzleSEL:@selector(setViewControllers:animated:) withSEL:@selector(swizzled_setViewControllers:animated:)]; }); } @@ -80,6 +81,18 @@ - (UIViewController *)swizzled_popViewControllerAnimated:(BOOL)animated { return poppedViewControllers; } +- (void)swizzled_setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated { + NSArray *prev = [self.viewControllers copy]; + [self swizzled_setViewControllers:viewControllers animated:animated]; + + NSSet *set = [NSSet setWithArray:viewControllers]; + for (UIViewController *v in prev) { + if (![set containsObject:v]) { + [v willDealloc]; + } + } +} + - (BOOL)willDealloc { if (![super willDealloc]) { return NO; diff --git a/MLeaksFinder/UIViewController+MemoryLeak.m b/MLeaksFinder/UIViewController+MemoryLeak.m index f7dee54..e1e05dc 100644 --- a/MLeaksFinder/UIViewController+MemoryLeak.m +++ b/MLeaksFinder/UIViewController+MemoryLeak.m @@ -49,7 +49,12 @@ - (void)swizzled_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(v if (!dismissedViewController) return; - [dismissedViewController willDealloc]; + // present出来的VC, dismiss的时候, 系统会多持有几秒? 为了避免频繁的误报, 给他多一点时间dealloc + __weak id weakVC = dismissedViewController; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + __strong id strongVC = weakVC; + [strongVC willDealloc]; + }); } - (BOOL)willDealloc {