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]
13 # define RR_PUSH() _objc_autoreleasePoolPush()
14 # define RR_POP(p) _objc_autoreleasePoolPop(p)
15 # define RR_RETAIN(o) _objc_rootRetain((id)o)
16 # define RR_RELEASE(o) _objc_rootRelease((id)o)
17 # define RR_AUTORELEASE(o) _objc_rootAutorelease((id)o)
20 #include <objc/objc-internal.h>
21 #include <Foundation/Foundation.h>
25 #define NESTED_COUNT 8
27 @interface Deallocator : NSObject @end
28 @implementation Deallocator
31 // testprintf("-[Deallocator %p dealloc]\n", self);
37 @interface AutoreleaseDuringDealloc : NSObject @end
38 @implementation AutoreleaseDuringDealloc
42 RR_AUTORELEASE([[Deallocator alloc] init]);
47 @interface AutoreleasePoolDuringDealloc : NSObject @end
48 @implementation AutoreleasePoolDuringDealloc
52 for (int i = 0; i < NESTED_COUNT; i++) {
53 RR_AUTORELEASE([[Deallocator alloc] init]);
57 void *pool = RR_PUSH();
58 for (int i = 0; i < NESTED_COUNT; i++) {
59 RR_AUTORELEASE([[Deallocator alloc] init]);
63 // caller's pool again
64 for (int i = 0; i < NESTED_COUNT; i++) {
65 RR_AUTORELEASE([[Deallocator alloc] init]);
71 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
74 state += NESTED_COUNT;
76 // local pool, not popped
78 for (int i = 0; i < NESTED_COUNT; i++) {
79 RR_AUTORELEASE([[Deallocator alloc] init]);
87 void *autorelease_lots_fn(void *singlePool)
89 // Enough to blow out the stack if AutoreleasePoolPage is recursive.
90 const int COUNT = 1024*1024;
94 void **pools = (void**)malloc((COUNT+1) * sizeof(void*));
95 pools[p++] = RR_PUSH();
97 id obj = RR_AUTORELEASE([[Deallocator alloc] init]);
99 for (int i = 0; i < COUNT; i++) {
100 if (rand() % 1000 == 0 && !singlePool) {
101 pools[p++] = RR_PUSH();
103 RR_AUTORELEASE(RR_RETAIN(obj));
107 testassert(state == 0);
111 testassert(state == 0);
113 testassert(state == 1);
119 void *nsthread_fn(void *arg __unused)
121 [NSThread currentThread];
122 void *pool = RR_PUSH();
123 RR_AUTORELEASE([[Deallocator alloc] init]);
130 // Normal autorelease.
131 testprintf("-- Normal autorelease.\n");
133 void *pool = RR_PUSH();
135 RR_AUTORELEASE([[Deallocator alloc] init]);
136 testassert(state == 0);
138 testassert(state == 1);
141 // Autorelease during dealloc during autoreleasepool-pop.
142 // That autorelease is handled by the popping pool, not the one above it.
143 testprintf("-- Autorelease during dealloc during autoreleasepool-pop.\n");
145 void *pool = RR_PUSH();
147 RR_AUTORELEASE([[AutoreleaseDuringDealloc alloc] init]);
148 testassert(state == 0);
150 testassert(state == 2);
153 // Autorelease pool during dealloc during autoreleasepool-pop.
154 testprintf("-- Autorelease pool during dealloc during autoreleasepool-pop.\n");
156 void *pool = RR_PUSH();
158 RR_AUTORELEASE([[AutoreleasePoolDuringDealloc alloc] init]);
159 testassert(state == 0);
161 testassert(state == 4 * NESTED_COUNT);
164 // Top-level thread pool popped normally.
165 testprintf("-- Thread-level pool popped normally.\n");
169 void *pool = RR_PUSH();
170 RR_AUTORELEASE([[Deallocator alloc] init]);
173 testassert(state == 1);
176 // Top-level thread pool not popped.
177 // The runtime should clean it up.
181 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
185 testprintf("-- Thread-level pool not popped.\n");
190 RR_AUTORELEASE([[Deallocator alloc] init]);
193 testassert(state == 1);
197 // Intermediate pool not popped.
198 // Popping the containing pool should clean up the skipped pool first.
202 if (!warned) testwarn("rdar://7138159 NSAutoreleasePool leaks");
206 testprintf("-- Intermediate pool not popped.\n");
208 void *pool = RR_PUSH();
209 void *pool2 = RR_PUSH();
210 RR_AUTORELEASE([[Deallocator alloc] init]);
212 (void)pool2; // pool2 not popped
214 testassert(state == 1);
220 // NSThread calls NSPopAutoreleasePool(0)
221 // rdar://9167170 but that currently breaks CF
224 if (!warned) testwarn("rdar://9167170 ignore NSPopAutoreleasePool(0)");
228 testprintf("-- pop(0).\n");
232 RR_AUTORELEASE([[AutoreleaseDuringDealloc alloc] init]);
233 testassert(state == 0);
235 testassert(state == 2);
243 // inflate the refcount side table so it doesn't show up in leak checks
246 id *objs = (id *)malloc(count*sizeof(id));
247 for (int i = 0; i < count; i++) {
248 objs[i] = RR_RETAIN([NSObject new]);
250 for (int i = 0; i < count; i++) {
258 // inflate NSAutoreleasePool's instance cache
261 id *objs = (id *)malloc(count * sizeof(id));
262 for (int i = 0; i < count; i++) {
263 objs[i] = [[NSAutoreleasePool alloc] init];
265 for (int i = 0; i < count; i++) {
266 [objs[count-i-1] release];
274 pthread_attr_t smallstack;
275 pthread_attr_init(&smallstack);
276 pthread_attr_setstacksize(&smallstack, 4096*4);
278 for (int i = 0; i < 100; i++) {
284 for (int i = 0; i < 1000; i++) {
290 // Large autorelease stack.
291 // Do this only once because it's slow.
292 testprintf("-- Large autorelease stack.\n");
294 // limit stack size: autorelease pop should not be recursive
296 pthread_create(&th, &smallstack, &autorelease_lots_fn, NULL);
297 pthread_join(th, NULL);
300 // Single large autorelease pool.
301 // Do this only once because it's slow.
302 testprintf("-- Large autorelease pool.\n");
304 // limit stack size: autorelease pop should not be recursive
306 pthread_create(&th, &smallstack, &autorelease_lots_fn, (void*)1);
307 pthread_join(th, NULL);
314 // Can't leak check this because it's too noisy.
315 testprintf("-- NSThread.\n");
318 pthread_create(&th, &smallstack, &nsthread_fn, 0);
319 pthread_join(th, NULL);
322 // NO LEAK CHECK HERE