From ca1d45a4e688c212d4ddb3964968650eff73717e Mon Sep 17 00:00:00 2001 From: David Chisnall Date: Sat, 14 Apr 2018 09:04:40 +0100 Subject: [PATCH] Fix dangling reference in weak ref code. Another test added as a result of coverage analysis. --- Test/ARCTest_arc.m | 35 +++++++++++++++++++++++++++++++++++ Test/CMakeLists.txt | 1 + arc.m | 1 + 3 files changed, 37 insertions(+) create mode 100644 Test/ARCTest_arc.m diff --git a/Test/ARCTest_arc.m b/Test/ARCTest_arc.m new file mode 100644 index 0000000..3271cc6 --- /dev/null +++ b/Test/ARCTest_arc.m @@ -0,0 +1,35 @@ +#import "Test.h" + +id __weak var; + +@interface ARC : Test @end +@implementation ARC +- (id)loadWeak +{ + return var; +} +- (void)setWeakFromWeak: (id __weak)anObject +{ + var = anObject; +} +- (void)setWeak: (id)anObject +{ + var = anObject; +} +@end + + +int main(void) +{ + ARC *obj = [ARC new]; + { + id o1 = [Test new]; + id o2 = [Test new]; + [obj setWeak: o1]; + assert([obj loadWeak] == o1); + [obj setWeakFromWeak: o2]; + assert([obj loadWeak] == o2); + } + assert([obj loadWeak] == nil); + return 0; +} diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt index 4165d29..fcc07e4 100644 --- a/Test/CMakeLists.txt +++ b/Test/CMakeLists.txt @@ -7,6 +7,7 @@ set(TESTS alignTest.m AllocatePair.m + ARCTest_arc.m AssociatedObject.m AssociatedObject2.m BlockImpTest.m diff --git a/arc.m b/arc.m index 0540391..0cc0e64 100644 --- a/arc.m +++ b/arc.m @@ -671,6 +671,7 @@ static inline BOOL weakRefRelease(WeakRef *ref) ref->weak_count--; if (ref->weak_count == 0) { + weak_ref_remove(weakRefs, ref->obj); free(ref); return YES; }