From 868afcbc82695f0f15dad6e45133ebdfed8840cc Mon Sep 17 00:00:00 2001 From: Jichao Wu Date: Sat, 26 Nov 2016 21:03:49 +0800 Subject: [PATCH 1/3] Support UINavigationController setViewControllers:animated: --- MLeaksFinder/UINavigationController+MemoryLeak.m | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/MLeaksFinder/UINavigationController+MemoryLeak.m b/MLeaksFinder/UINavigationController+MemoryLeak.m index 94e2add..bd1b765 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:YES]; + + NSSet *set = [NSSet setWithArray:viewControllers]; + for (UIViewController *v in prev) { + if (![set containsObject:v]) { + [v willDealloc]; + } + } +} + - (BOOL)willDealloc { if (![super willDealloc]) { return NO; From c155fe3620483ac600edcbc44a7b22ede0e79c70 Mon Sep 17 00:00:00 2001 From: Jichao Wu Date: Tue, 17 Jan 2017 20:23:47 +0800 Subject: [PATCH 2/3] fix typo --- MLeaksFinder/UINavigationController+MemoryLeak.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MLeaksFinder/UINavigationController+MemoryLeak.m b/MLeaksFinder/UINavigationController+MemoryLeak.m index bd1b765..099f1f1 100644 --- a/MLeaksFinder/UINavigationController+MemoryLeak.m +++ b/MLeaksFinder/UINavigationController+MemoryLeak.m @@ -83,7 +83,7 @@ - (UIViewController *)swizzled_popViewControllerAnimated:(BOOL)animated { - (void)swizzled_setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated { NSArray *prev = [self.viewControllers copy]; - [self swizzled_setViewControllers:viewControllers animated:YES]; + [self swizzled_setViewControllers:viewControllers animated:animated]; NSSet *set = [NSSet setWithArray:viewControllers]; for (UIViewController *v in prev) { From 5374a54b467855d3e41a2c1717fe7b96d6968316 Mon Sep 17 00:00:00 2001 From: Jichao Wu Date: Sun, 23 Apr 2017 14:57:56 +0800 Subject: [PATCH 3/3] =?UTF-8?q?present=E5=87=BA=E6=9D=A5=E7=9A=84VC,=20dis?= =?UTF-8?q?miss=E7=9A=84=E6=97=B6=E5=80=99,=20=E7=B3=BB=E7=BB=9F=E4=BC=9A?= =?UTF-8?q?=E5=A4=9A=E6=8C=81=E6=9C=89=E5=87=A0=E7=A7=92=3F=20=E4=B8=BA?= =?UTF-8?q?=E4=BA=86=E9=81=BF=E5=85=8D=E9=A2=91=E7=B9=81=E7=9A=84=E8=AF=AF?= =?UTF-8?q?=E6=8A=A5,=20=E7=BB=99=E4=BB=96=E5=A4=9A=E4=B8=80=E7=82=B9?= =?UTF-8?q?=E6=97=B6=E9=97=B4dealloc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MLeaksFinder/UIViewController+MemoryLeak.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 {