Fix weak ref handling during dealloc (#215)

Add test for setting weak ref during dealloc

Fixes #214
main
Frederik Seiffert 4 years ago committed by GitHub
parent bdf03cb55d
commit b32ee7787d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -60,6 +60,17 @@ id __weak var;
} }
@end @end
static __weak id weakRef;
@interface CheckDeallocWeakRef : Test
@end
@implementation CheckDeallocWeakRef
- (void)dealloc
{
weakRef = self;
}
@end
int main(void) int main(void)
{ {
@ -115,5 +126,10 @@ int main(void)
[obj setWeak: x]; [obj setWeak: x];
} }
assert([obj loadWeak] != nil); assert([obj loadWeak] != nil);
// Check setting weak references during dealloc
{
[CheckDeallocWeakRef new];
}
assert(weakRef == nil);
return 0; return 0;
} }

@ -838,6 +838,12 @@ extern "C" OBJC_PUBLIC id objc_storeWeak(id *addr, id obj)
*addr = obj; *addr = obj;
return obj; return obj;
} }
// If the object is being deallocated return nil.
if (object_getRetainCount_np(obj) == 0)
{
*addr = nil;
return nil;
}
if (nil != obj) if (nil != obj)
{ {
*addr = (id)incrementWeakRefCount(obj); *addr = (id)incrementWeakRefCount(obj);
@ -991,6 +997,12 @@ extern "C" OBJC_PUBLIC id objc_initWeak(id *addr, id obj)
*addr = obj; *addr = obj;
return obj; return obj;
} }
// If the object is being deallocated return nil.
if (object_getRetainCount_np(obj) == 0)
{
*addr = nil;
return nil;
}
if (nil != obj) if (nil != obj)
{ {
*(WeakRef**)addr = incrementWeakRefCount(obj); *(WeakRef**)addr = incrementWeakRefCount(obj);

Loading…
Cancel
Save