Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions MLeaksFinder/UINavigationController+MemoryLeak.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:)];
});
}

Expand Down Expand Up @@ -80,6 +81,18 @@ - (UIViewController *)swizzled_popViewControllerAnimated:(BOOL)animated {
return poppedViewControllers;
}

- (void)swizzled_setViewControllers:(NSArray<UIViewController *> *)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;
Expand Down
7 changes: 6 additions & 1 deletion MLeaksFinder/UIViewController+MemoryLeak.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down