1 // Define FOUNDATION=1 for NSObject and NSAutoreleasePool
2 // Define FOUNDATION=0 for _objc_root* and _objc_autoreleasePool*
7 # define RR_PUSH() [[NSAutoreleasePool alloc] init]
8 # define RR_POP(p) [(id)p release]
9 # define RR_RETAIN(o) [o retain]
10 # define RR_RELEASE(o) [o release]
11 # define RR_AUTORELEASE(o) [o autorelease]
12 # define RR_RETAINCOUNT(o) [o retainCount]
14 # define RR_PUSH() _objc_autoreleasePoolPush()
15 # define RR_POP(p) _objc_autoreleasePoolPop(p)
16 # define RR_RETAIN(o) _objc_rootRetain((id)o)
17 # define RR_RELEASE(o) _objc_rootRelease((id)o)
18 # define RR_AUTORELEASE(o) _objc_rootAutorelease((id)o)
19 # define RR_RETAINCOUNT(o) _objc_rootRetainCount((id)o)
22 #include <objc/objc-internal.h>
23 #include <Foundation/Foundation.h>
26 static pthread_attr_t smallstack;
28 #define NESTED_COUNT 8
30 @interface Deallocator : NSObject @end
31 @implementation Deallocator
34 // testprintf("-[Deallocator %p dealloc]\n", self);
40 @interface AutoreleaseDuringDealloc : NSObject @end
41 @implementation AutoreleaseDuringDealloc
45 RR_AUTORELEASE([[Deallocator alloc] init]);
50 @interface AutoreleasePoolDuringDealloc : NSObject @end
51 @implementation AutoreleasePoolDuringDealloc
55 for (int i = 0; i < NESTED_COUNT; i++) {
56 RR_AUTORELEASE([[Deallocator alloc] init]);
60 void *pool = RR_PUSH();
61 for (int i = 0; i < NESTED_COUNT; i++) {
62 RR_AUTORELEASE([[Deallocator alloc] init]);
66 // caller's pool again
67 for (int i = 0; i < NESTED_COUNT; i++) {
68 RR_AUTORELEASE([[Deallocator alloc] init]);
74 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
77 state += NESTED_COUNT;
79 // local pool, not popped
81 for (int i = 0; i < NESTED_COUNT; i++) {
82 RR_AUTORELEASE([[Deallocator alloc] init]);
90 void *autorelease_lots_fn(void *singlePool)
92 // Enough to blow out the stack if AutoreleasePoolPage is recursive.
93 const int COUNT = 1024*1024;
97 void **pools = (void**)malloc((COUNT+1) * sizeof(void*));
98 pools[p++] = RR_PUSH();
100 id obj = RR_AUTORELEASE([[Deallocator alloc] init]);
102 // last pool has only 1 autorelease in it
103 pools[p++] = RR_PUSH();
105 for (int i = 0; i < COUNT; i++) {
106 if (rand() % 1000 == 0 && !singlePool) {
107 pools[p++] = RR_PUSH();
109 RR_AUTORELEASE(RR_RETAIN(obj));
113 testassert(state == 0);
117 testassert(state == 0);
118 testassert(RR_RETAINCOUNT(obj) == 1);
120 testassert(state == 1);
126 void *nsthread_fn(void *arg __unused)
128 [NSThread currentThread];
129 void *pool = RR_PUSH();
130 RR_AUTORELEASE([[Deallocator alloc] init]);
137 // Normal autorelease.
138 testprintf("-- Normal autorelease.\n");
140 void *pool = RR_PUSH();
142 RR_AUTORELEASE([[Deallocator alloc] init]);
143 testassert(state == 0);
145 testassert(state == 1);
148 // Autorelease during dealloc during autoreleasepool-pop.
149 // That autorelease is handled by the popping pool, not the one above it.
150 testprintf("-- Autorelease during dealloc during autoreleasepool-pop.\n");
152 void *pool = RR_PUSH();
154 RR_AUTORELEASE([[AutoreleaseDuringDealloc alloc] init]);
155 testassert(state == 0);
157 testassert(state == 2);
160 // Autorelease pool during dealloc during autoreleasepool-pop.
161 testprintf("-- Autorelease pool during dealloc during autoreleasepool-pop.\n");
163 void *pool = RR_PUSH();
165 RR_AUTORELEASE([[AutoreleasePoolDuringDealloc alloc] init]);
166 testassert(state == 0);
168 testassert(state == 4 * NESTED_COUNT);
171 // Top-level thread pool popped normally.
172 // Check twice - once for empty placeholder, once without.
173 # if DEBUG_POOL_ALLOCATION || FOUNDATION
174 // DebugPoolAllocation disables the empty placeholder pool.
175 // Guard Malloc disables the empty placeholder pool (checked at runtime)
176 // Foundation makes RR_PUSH return an NSAutoreleasePool not the raw token.
177 # define CHECK_PLACEHOLDER 0
179 # define CHECK_PLACEHOLDER 1
181 testprintf("-- Thread-level pool popped normally.\n");
185 void *pool = RR_PUSH();
186 #if CHECK_PLACEHOLDER
187 if (!is_guardmalloc()) {
188 testassert(pool == (void*)1);
191 RR_AUTORELEASE([[Deallocator alloc] init]);
194 #if CHECK_PLACEHOLDER
195 if (!is_guardmalloc()) {
196 testassert(pool != (void*)1);
199 RR_AUTORELEASE([[Deallocator alloc] init]);
202 testassert(state == 2);
206 // Autorelease with no pool.
207 testprintf("-- Autorelease with no pool.\n");
211 RR_AUTORELEASE([[Deallocator alloc] init]);
213 testassert(state == 1);
216 // Autorelease with no pool after popping the top-level pool.
217 testprintf("-- Autorelease with no pool after popping the last pool.\n");
221 void *pool = RR_PUSH();
222 RR_AUTORELEASE([[Deallocator alloc] init]);
224 RR_AUTORELEASE([[Deallocator alloc] init]);
226 testassert(state == 2);
229 // Top-level thread pool not popped.
230 // The runtime should clean it up.
234 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
238 testprintf("-- Thread-level pool not popped.\n");
243 RR_AUTORELEASE([[Deallocator alloc] init]);
246 testassert(state == 1);
250 // Intermediate pool not popped.
251 // Popping the containing pool should clean up the skipped pool first.
255 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
259 testprintf("-- Intermediate pool not popped.\n");
261 void *pool = RR_PUSH();
262 void *pool2 = RR_PUSH();
263 RR_AUTORELEASE([[Deallocator alloc] init]);
265 (void)pool2; // pool2 not popped
267 testassert(state == 1);
276 // Large autorelease stack.
277 // Do this only once because it's slow.
278 testprintf("-- Large autorelease stack.\n");
280 // limit stack size: autorelease pop should not be recursive
282 pthread_create(&th, &smallstack, &autorelease_lots_fn, NULL);
283 pthread_join(th, NULL);
286 // Single large autorelease pool.
287 // Do this only once because it's slow.
288 testprintf("-- Large autorelease pool.\n");
290 // limit stack size: autorelease pop should not be recursive
292 pthread_create(&th, &smallstack, &autorelease_lots_fn, (void*)1);
293 pthread_join(th, NULL);
300 pthread_attr_init(&smallstack);
301 pthread_attr_setstacksize(&smallstack, 32768);
303 // inflate the refcount side table so it doesn't show up in leak checks
306 id *objs = (id *)malloc(count*sizeof(id));
307 for (int i = 0; i < count; i++) {
308 objs[i] = RR_RETAIN([NSObject new]);
310 for (int i = 0; i < count; i++) {
318 // inflate NSAutoreleasePool's instance cache
321 id *objs = (id *)malloc(count * sizeof(id));
322 for (int i = 0; i < count; i++) {
323 objs[i] = [[NSAutoreleasePool alloc] init];
325 for (int i = 0; i < count; i++) {
326 [objs[count-i-1] release];
335 for (int i = 0; i < 100; i++) {
342 // check for leaks using top-level pools
346 for (int i = 0; i < 1000; i++) {
357 // check for leaks using pools not at top level
358 // fixme for FOUNDATION this leak mark/check needs
359 // to be outside the autorelease pool for some reason
361 void *pool = RR_PUSH();
363 for (int i = 0; i < 1000; i++) {
373 // Can't leak check this because it's too noisy.
374 testprintf("-- NSThread.\n");
377 pthread_create(&th, &smallstack, &nsthread_fn, 0);
378 pthread_join(th, NULL);
381 // NO LEAK CHECK HERE