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
/
Copy pathMSViewLeakHunter.m
80 lines (60 loc) · 1.7 KB
/
MSViewLeakHunter.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
//
// MSViewLeakHunter.m
// MindSnacks
//
// Created by Javier Soto on 12/7/12.
//
//
#import "MSViewLeakHunter.h"
#import "MSLeakHunter+Private.h"
#if MSLeakHunter_ENABLED
#if MSViewLeakHunter_ENABLED
@interface UIView (MSViewLeakHunter)
- (void)_msviewLeakHunter_didMoveToWindow;
- (void)_msviewLeakHunter_dealloc;
@end
@implementation MSViewLeakHunter
+ (void)install
{
Class class = [UIView class];
[MSLeakHunter swizzleMethod:@selector(didMoveToWindow)
ofClass:class
withMethod:@selector(_msviewLeakHunter_didMoveToWindow)];
[MSLeakHunter swizzleMethod:NSSelectorFromString(@"dealloc")
ofClass:class
withMethod:@selector(_msviewLeakHunter_dealloc)];
}
@end
@implementation UIView (MSViewLeakHunter)
/**
* @return a string that identifies the controller. This is used to be pased to `MSVCLeakHunter` without retaining the controller.
*/
- (NSString *)viewReferenceString
{
return [NSString stringWithFormat:@"VIEW %@ <%p>", NSStringFromClass([self class]), self];
}
- (void)cancelLeakCheck
{
[MSLeakHunter cancelLeakNotificationWithObjectReferenceString:[self viewReferenceString]];
}
- (void)_msviewLeakHunter_didMoveToWindow
{
if (!self.window)
{
[MSLeakHunter scheduleLeakNotificationWithObjectReferenceString:[self viewReferenceString]
afterDelay:kMSViewLeakHunterDisappearAndDeallocateMaxInterval];
}
else
{
[self cancelLeakCheck];
}
}
- (void)_msviewLeakHunter_dealloc
{
[self cancelLeakCheck];
// Call original implementation
[self _msviewLeakHunter_dealloc];
}
@end
#endif
#endif