-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathcustomDeallocInitiation.m
84 lines (64 loc) · 2.3 KB
/
customDeallocInitiation.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
// TEST_CONFIG MEM=mrc
#include "test.h"
#include <objc/NSObject.h>
static int deallocCalls;
static int initiateDeallocCalls;
@interface SuperTest: NSObject
@end
@implementation SuperTest
- (id)init {
deallocCalls = 0;
initiateDeallocCalls = 0;
return self;
}
- (void)dealloc {
deallocCalls++;
[super dealloc];
}
- (void)_objc_initiateDealloc {
initiateDeallocCalls++;
[super dealloc];
}
@end
@interface SubTestRealizedBeforeSet: SuperTest @end
@implementation SubTestRealizedBeforeSet @end
@interface SubTestRealizedAfterSet: SuperTest @end
@implementation SubTestRealizedAfterSet @end
int main()
{
// Realize classes and test the normal dealloc path.
[[SuperTest new] release];
testassertequal(deallocCalls, 1);
testassertequal(initiateDeallocCalls, 0);
[[SubTestRealizedBeforeSet new] release];
testassertequal(deallocCalls, 1);
testassertequal(initiateDeallocCalls, 0);
Class DynamicSubTestCreatedBeforeSet = objc_allocateClassPair(
[SuperTest class], "DynamicSubTestCreatedBeforeSet", 0);
objc_registerClassPair(DynamicSubTestCreatedBeforeSet);
[[DynamicSubTestCreatedBeforeSet new] release];
testassertequal(deallocCalls, 1);
testassertequal(initiateDeallocCalls, 0);
// Set custom dealloc initiation and test the above classes again.
_class_setCustomDeallocInitiation([SuperTest class]);
[[SuperTest new] release];
testassertequal(deallocCalls, 0);
testassertequal(initiateDeallocCalls, 1);
[[SubTestRealizedBeforeSet new] release];
testassertequal(deallocCalls, 0);
testassertequal(initiateDeallocCalls, 1);
[[DynamicSubTestCreatedBeforeSet new] release];
testassertequal(deallocCalls, 0);
testassertequal(initiateDeallocCalls, 1);
// Test subclasses that are realized or created after setting custom initiation.
[[SubTestRealizedAfterSet new] release];
testassertequal(deallocCalls, 0);
testassertequal(initiateDeallocCalls, 1);
Class DynamicSubTestCreatedAfterSet = objc_allocateClassPair(
[SuperTest class], "DynamicSubTestCreatedAfterSet", 0);
objc_registerClassPair(DynamicSubTestCreatedAfterSet);
[[DynamicSubTestCreatedAfterSet new] release];
testassertequal(deallocCalls, 0);
testassertequal(initiateDeallocCalls, 1);
succeed(__FILE__);
}