Skip to content

Commit d755b38

Browse files
committed
[Runtime] Teach BridgeObjectBox::retain() not to drop the "not native" bit.
The "not native" bit in a BridgeObject is important, because it tells us when we need to go through the Objective-C -retain method vs. swift_retain. Losing the bit means that swift_retain() will stomp on some memory within an Objective-C object, thinking its the inline reference count. Co-debugged with Arnold, who then found where this bit was getting dropped. Fixes rdar://problem/39629937.
1 parent b8d1a06 commit d755b38

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

stdlib/public/runtime/MetadataImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ struct BridgeObjectBox :
410410
static constexpr unsigned numExtraInhabitants = 1;
411411

412412
static void *retain(void *obj) {
413-
return swift_bridgeObjectRetain(obj);
413+
(void)swift_bridgeObjectRetain(obj);
414+
return obj;
414415
}
415416

416417
static void release(void *obj) {

test/stdlib/Mirror.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,30 @@ mirrors.test("ObjC") {
480480
expectEqual(0, Mirror(reflecting: HasIVars()).children.count)
481481
}
482482

483+
// rdar://problem/39629937
484+
@objc class ObjCClass : NSObject {
485+
let value: Int
486+
487+
init(value: Int) { self.value = value }
488+
489+
override var description: String {
490+
return "\(value)"
491+
}
492+
}
493+
494+
struct WrapObjCClassArray {
495+
var array: [ObjCClass]
496+
}
497+
498+
mirrors.test("struct/WrapNSArray") {
499+
let nsArray: NSArray = [
500+
ObjCClass(value: 1), ObjCClass(value: 2),
501+
ObjCClass(value: 3), ObjCClass(value: 4)
502+
]
503+
let s = String(describing: WrapObjCClassArray(array: nsArray as! [ObjCClass]))
504+
expectEqual("WrapObjCClassArray(array: [1, 2, 3, 4])", s)
505+
}
506+
483507
#endif // _runtime(_ObjC)
484508

485509
//===--- Suppressed Superclass Mirrors ------------------------------------===//

0 commit comments

Comments
 (0)