RetainCountExamples.m
3.02 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-config ipa=dynamic-bifurcate -verify %s
typedef signed char BOOL;
typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
@protocol NSObject - (BOOL)isEqual:(id)object; @end
@interface NSObject <NSObject> {}
+(id)alloc;
+(id)new;
- (oneway void)release;
-(id)init;
-(id)autorelease;
-(id)copy;
- (Class)class;
-(id)retain;
- (oneway void)release;
@end
@interface SelfStaysLive : NSObject
- (id)init;
@end
@implementation SelfStaysLive
- (id)init {
return [super init];
}
@end
void selfStaysLive() {
SelfStaysLive *foo = [[SelfStaysLive alloc] init];
[foo release];
}
// Test that retain release checker warns on leaks and use-after-frees when
// self init is not enabled.
// radar://12115830
@interface ParentOfCell : NSObject
- (id)initWithInt: (int)inInt;
@end
@interface Cell : ParentOfCell{
int x;
}
- (id)initWithInt: (int)inInt;
+ (void)testOverRelease;
+ (void)testLeak;
@property int x;
@end
@implementation Cell
@synthesize x;
- (id) initWithInt: (int)inInt {
[super initWithInt: inInt];
self.x = inInt; // no-warning
return self; // Self Init checker would produce a warning here.
}
+ (void) testOverRelease {
Cell *sharedCell3 = [[Cell alloc] initWithInt: 3];
[sharedCell3 release];
[sharedCell3 release]; // expected-warning {{Reference-counted object is used after it is released}}
}
+ (void) testLeak {
Cell *sharedCell4 = [[Cell alloc] initWithInt: 3]; // expected-warning {{leak}}
}
@end
// We should stop tracking some objects even when we inline the call.
// Specialically, the objects passed into calls with delegate and callback
// parameters.
@class DelegateTest;
typedef void (*ReleaseCallbackTy) (DelegateTest *c);
@interface Delegate : NSObject
@end
@interface DelegateTest : NSObject {
Delegate *myDel;
}
// Object initialized with a delagate which could potentially release it.
- (id)initWithDelegate: (id) d;
- (void) setDelegate: (id) d;
// Releases object through callback.
+ (void)updateObject:(DelegateTest*)obj WithCallback:(ReleaseCallbackTy)rc;
+ (void)test: (Delegate *)d;
@property (assign) Delegate* myDel;
@end
void releaseObj(DelegateTest *c);
// Releases object through callback.
void updateObject(DelegateTest *c, ReleaseCallbackTy rel) {
rel(c);
}
@implementation DelegateTest
@synthesize myDel;
- (id) initWithDelegate: (id) d {
if ((self = [super init]))
myDel = d;
return self;
}
- (void) setDelegate: (id) d {
myDel = d;
}
+ (void)updateObject:(DelegateTest*)obj WithCallback:(ReleaseCallbackTy)rc {
rc(obj);
}
+ (void) test: (Delegate *)d {
DelegateTest *obj1 = [[DelegateTest alloc] initWithDelegate: d]; // no-warning
DelegateTest *obj2 = [[DelegateTest alloc] init]; // no-warning
DelegateTest *obj3 = [[DelegateTest alloc] init]; // no-warning
updateObject(obj2, releaseObj);
[DelegateTest updateObject: obj3
WithCallback: releaseObj];
DelegateTest *obj4 = [[DelegateTest alloc] init]; // no-warning
[obj4 setDelegate: d];
}
@end