4 #include <Foundation/NSObject.h>
5 #include <objc/runtime.h>
11 static const char *key = "key";
14 @interface Value : NSObject @end
15 @interface Super : NSObject @end
16 @interface Sub : NSObject @end
18 @interface Super2 : NSObject @end
19 @interface Sub2 : NSObject @end
24 // rdar://8270243 don't lose associations after isa swizzling
26 id value = [Value new];
27 objc_setAssociatedObject(self, &key, value, OBJC_ASSOCIATION_RETAIN);
30 object_setClass(self, [Sub class]);
51 @implementation Super2
54 // rdar://9617109 don't lose associations after isa swizzling
56 id value = [Value new];
57 object_setClass(self, [Sub2 class]);
58 objc_setAssociatedObject(self, &key, value, OBJC_ASSOCIATION_RETAIN);
60 object_setClass(self, [Super2 class]);
93 for (i = 0; i < 100; i++) {
94 RELEASE_VALUE([[Super alloc] init]);
99 testassert(supers == 0);
100 testassert(subs > 0);
101 testassert(subs == values);
110 for (i = 0; i < 100; i++) {
111 RELEASE_VALUE([[Super2 alloc] init]);
116 testassert(supers > 0);
117 testassert(subs == 0);
118 testassert(supers == values);
120 // rdar://44094390 tolerate nil object and nil value
121 #pragma clang diagnostic push
122 #pragma clang diagnostic ignored "-Wnonnull"
123 objc_setAssociatedObject(nil, &key, nil, OBJC_ASSOCIATION_ASSIGN);
124 #pragma clang diagnostic pop