5 objc\[\d+\]: Cannot form weak reference to instance \(0x[0-9a-f]+\) of class Crash. It is possible that this object was over-released, or is in the process of deallocation.
12 #include <Foundation/NSObject.h>
18 static bool did_dealloc;
22 @interface NSObject (WeakInternals)
24 -(BOOL)_isDeallocating;
27 @interface Test : NSObject @end
30 testprintf("Weak storeOrNil does not crash while deallocating\n");
31 weak4 = (id)0x100; // old value must not be used
32 id result = objc_initWeakOrNil(&weak4, self);
33 testassert(result == nil);
34 testassert(weak4 == nil);
35 result = objc_storeWeakOrNil(&weak4, self);
36 testassert(result == nil);
37 testassert(weak4 == nil);
39 // The value returned by objc_loadWeak() is now nil,
40 // but the storage is not yet cleared.
41 testassert(weak == self);
42 testassert(weak2 == self);
44 // objc_loadWeak() does not eagerly clear the storage.
45 testassert(objc_loadWeakRetained(&weak) == nil);
46 testassert(weak != nil);
48 // dealloc clears the storage.
49 testprintf("Weak references clear during super dealloc\n");
50 testassert(weak2 != nil);
52 testassert(weak == nil);
53 testassert(weak2 == nil);
59 @interface CustomTryRetain : Test @end
60 @implementation CustomTryRetain
61 -(BOOL)_tryRetain { state++; return [super _tryRetain]; }
64 @interface CustomIsDeallocating : Test @end
65 @implementation CustomIsDeallocating
66 -(BOOL)_isDeallocating { state++; return [super _isDeallocating]; }
69 @interface CustomAllowsWeakReference : Test @end
70 @implementation CustomAllowsWeakReference
71 -(BOOL)allowsWeakReference { state++; return [super allowsWeakReference]; }
74 @interface CustomRetainWeakReference : Test @end
75 @implementation CustomRetainWeakReference
76 -(BOOL)retainWeakReference { state++; return [super retainWeakReference]; }
79 @interface Crash : NSObject @end
82 testassert(weak == self);
83 testassert(weak2 == self);
84 testassert(objc_loadWeakRetained(&weak) == nil);
85 testassert(objc_loadWeakRetained(&weak2) == nil);
87 testprintf("Weak storeOrNil does not crash while deallocating\n");
88 id result = objc_storeWeakOrNil(&weak, self);
89 testassert(result == nil);
91 testprintf("Weak store crashes while deallocating\n");
92 objc_storeWeak(&weak, self);
93 fail("objc_storeWeak of deallocating value should have crashed");
99 void cycle(Class cls, Test *obj, Test *obj2, bool storeOrNil)
101 testprintf("Cycling class %s\n", class_getName(cls));
105 id (*storeWeak)(id *location, id obj);
106 id (*initWeak)(id *location, id obj);
108 testprintf("Using objc_storeWeakOrNil\n");
109 storeWeak = objc_storeWeakOrNil;
110 initWeak = objc_initWeakOrNil;
112 testprintf("Using objc_storeWeak\n");
113 storeWeak = objc_storeWeak;
114 initWeak = objc_initWeak;
117 // state counts calls to custom weak methods
118 // Difference test classes have different expected values.
121 if (cls == [Test class]) {
125 else if (cls == [CustomTryRetain class] ||
126 cls == [CustomRetainWeakReference class])
131 else if (cls == [CustomIsDeallocating class] ||
132 cls == [CustomAllowsWeakReference class])
139 testprintf("Weak assignment\n");
141 result = storeWeak(&weak, obj);
142 testassert(state == storeTarget);
143 testassert(result == obj);
144 testassert(weak == obj);
146 testprintf("Weak assignment to the same value\n");
148 result = storeWeak(&weak, obj);
149 testassert(state == storeTarget);
150 testassert(result == obj);
151 testassert(weak == obj);
153 testprintf("Weak load\n");
155 result = objc_loadWeakRetained(&weak);
156 if (state != loadTarget) testprintf("state %d target %d\n", state, loadTarget);
157 testassert(state == loadTarget);
158 testassert(result == obj);
159 testassert(result == weak);
162 testprintf("Weak assignment to different value\n");
164 result = storeWeak(&weak, obj2);
165 testassert(state == storeTarget);
166 testassert(result == obj2);
167 testassert(weak == obj2);
169 testprintf("Weak assignment to NULL\n");
171 result = storeWeak(&weak, NULL);
172 testassert(state == 0);
173 testassert(result == NULL);
174 testassert(weak == NULL);
176 testprintf("Weak re-assignment to NULL\n");
178 result = storeWeak(&weak, NULL);
179 testassert(state == 0);
180 testassert(result == NULL);
181 testassert(weak == NULL);
183 testprintf("Weak move\n");
185 result = storeWeak(&weak, obj);
186 testassert(state == storeTarget);
187 testassert(result == obj);
188 testassert(weak == obj);
189 weak2 = (id)(PAGE_MAX_SIZE-16);
190 objc_moveWeak(&weak2, &weak);
191 testassert(weak == nil);
192 testassert(weak2 == obj);
193 storeWeak(&weak2, NULL);
195 testprintf("Weak copy\n");
197 result = storeWeak(&weak, obj);
198 testassert(state == storeTarget);
199 testassert(result == obj);
200 testassert(weak == obj);
201 weak2 = (id)(PAGE_MAX_SIZE-16);
202 objc_copyWeak(&weak2, &weak);
203 testassert(weak == obj);
204 testassert(weak2 == obj);
205 storeWeak(&weak, NULL);
206 storeWeak(&weak2, NULL);
208 testprintf("Weak clear\n");
213 result = storeWeak(&weak, obj3);
214 testassert(state == storeTarget);
215 testassert(result == obj3);
216 testassert(weak == obj3);
219 result = storeWeak(&weak2, obj3);
220 testassert(state == storeTarget);
221 testassert(result == obj3);
222 testassert(weak2 == obj3);
226 testassert(did_dealloc);
227 testassert(weak == NULL);
228 testassert(weak2 == NULL);
231 testprintf("Weak init and destroy\n");
236 weak = (id)0x100; // old value must not be used
237 result = initWeak(&weak, obj4);
238 testassert(state == storeTarget);
239 testassert(result == obj4);
240 testassert(weak == obj4);
243 weak2 = (id)0x100; // old value must not be used
244 result = initWeak(&weak2, obj4);
245 testassert(state == storeTarget);
246 testassert(result == obj4);
247 testassert(weak2 == obj4);
250 weak3 = (id)0x100; // old value must not be used
251 result = initWeak(&weak3, obj4);
252 testassert(state == storeTarget);
253 testassert(result == obj4);
254 testassert(weak3 == obj4);
257 objc_destroyWeak(&weak3);
258 testassert(state == 0);
259 testassert(weak3 == obj4); // storage is unchanged
263 testassert(did_dealloc);
264 testassert(weak == NULL); // not destroyed earlier so cleared now
265 testassert(weak2 == NULL); // not destroyed earlier so cleared now
266 testassert(weak3 == obj4); // destroyed earlier so not cleared now
268 objc_destroyWeak(&weak);
269 objc_destroyWeak(&weak2);
273 void test_class(Class cls)
275 // prime strong and weak side tables before leak checking
276 Test *prime[256] = {nil};
277 for (size_t i = 0; i < sizeof(prime)/sizeof(prime[0]); i++) {
278 objc_storeWeak(&prime[i], [cls new]);
281 Test *obj = [cls new];
282 Test *obj2 = [cls new];
284 for (int i = 0; i < 100000; i++) {
285 cycle(cls, obj, obj2, false);
286 cycle(cls, obj, obj2, true);
289 for (int i = 0; i < 100000; i++) {
290 cycle(cls, obj, obj2, false);
291 cycle(cls, obj, obj2, true);
293 // allow some slop for side table expansion
294 // 5120 is common with this configuration
299 for (size_t i = 0; i < sizeof(weaks)/sizeof(weaks[0]); i++) {
300 objc_storeWeak(&weaks[i], obj);
302 for (size_t i = 0; i < sizeof(weaks)/sizeof(weaks[0]); i++) {
303 objc_storeWeak(&weaks[i], nil);
309 test_class([Test class]);
310 test_class([CustomTryRetain class]);
311 test_class([CustomIsDeallocating class]);
312 test_class([CustomAllowsWeakReference class]);
313 test_class([CustomRetainWeakReference class]);
318 Crash *obj3 = [Crash new];
319 result = objc_storeWeak(&weak, obj3);
320 testassert(result == obj3);
321 testassert(weak == obj3);
323 result = objc_storeWeak(&weak2, obj3);
324 testassert(result == obj3);
325 testassert(weak2 == obj3);
328 fail("should have crashed in -[Crash dealloc]");