This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
MSViewControllerLeakHunter.m
105 lines (76 loc) · 2.57 KB
/
MSViewControllerLeakHunter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// MSVCLeakHunter.m
// MSAppKit
//
// Created by Javier Soto on 10/16/12.
//
//
#import "MSViewControllerLeakHunter.h"
#import "MSLeakHunter+Private.h"
#if MSLeakHunter_ENABLED
#if MSVCLeakHunter_EnableUIViewControllerLog
#define LOG_METHOD(NAME) NSLog(@"MSViewControllerLeakHunter -[%@ %@]", NSStringFromClass([self class]), NAME)
#else
#define LOG_METHOD(NAME)
#endif
@interface UIViewController (MSViewControllerLeakHunter)
- (void)_msvcLeakHunter_viewDidAppear:(BOOL)animated;
- (void)_msvcLeakHunter_viewDidDisappear:(BOOL)animated;
- (void)_msvcLeakHunter_dealloc;
@end
@implementation MSViewControllerLeakHunter
+ (void)install
{
Class class = [UIViewController class];
[MSLeakHunter swizzleMethod:@selector(viewDidAppear:)
ofClass:class
withMethod:@selector(_msvcLeakHunter_viewDidAppear:)];
[MSLeakHunter swizzleMethod:@selector(viewDidDisappear:)
ofClass:class
withMethod:@selector(_msvcLeakHunter_viewDidDisappear:)];
[MSLeakHunter swizzleMethod:NSSelectorFromString(@"dealloc")
ofClass:class
withMethod:@selector(_msvcLeakHunter_dealloc)];
}
@end
@implementation UIViewController (MSViewControllerLeakHunter)
/**
* @return a string that identifies the controller. This is used to be pased to `MSVCLeakHunter` without retaining the controller.
*/
- (NSString *)controllerReferenceString
{
return [NSString stringWithFormat:@"VIEW CONTROLLER %@ <%p>", NSStringFromClass([self class]), self];
}
- (void)cancelLeakCheck
{
[MSLeakHunter cancelLeakNotificationWithObjectReferenceString:[self controllerReferenceString]];
}
- (void)scheduleLeakCheck
{
[MSLeakHunter scheduleLeakNotificationWithObjectReferenceString:[self controllerReferenceString]
afterDelay:kMSVCLeakHunterDisappearAndDeallocateMaxInterval];
}
#pragma mark - Alternative method implementations)
- (void)_msvcLeakHunter_viewDidAppear:(BOOL)animated
{
LOG_METHOD(@"viewDidAppear:");
[self cancelLeakCheck];
// Call original implementation
[self _msvcLeakHunter_viewDidAppear:animated];
}
- (void)_msvcLeakHunter_viewDidDisappear:(BOOL)animated
{
LOG_METHOD(@"viewDidDisappear:");
[self scheduleLeakCheck];
// Call original implementation
[self _msvcLeakHunter_viewDidDisappear:animated];
}
- (void)_msvcLeakHunter_dealloc
{
LOG_METHOD(@"dealloc");
[self cancelLeakCheck];
// Call original implementation
[self _msvcLeakHunter_dealloc];
}
@end
#endif