Skip to content

Commit 6ba5ebd

Browse files
authored
Merge pull request #19795 from mikeash/fix-mirror-weak-leak
[Runtime] Fix a leak when reading a weak property with Mirror.
2 parents 6c57130 + 101c7cb commit 6ba5ebd

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

stdlib/public/runtime/ReflectionMirror.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ static bool loadSpecialReferenceStorage(OpaqueValue *fieldData,
172172
reinterpret_cast<OpaqueValue *>(temporaryValue));
173173

174174
type->deallocateBufferIn(&temporaryBuffer);
175+
swift_unknownObjectRelease(strongValue);
175176

176177
return true;
177178
}

test/stdlib/WeakMirror.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ mirrors.test("struct/StructHasNativeWeakReference") {
9999
print(extractedChild)
100100
}
101101

102+
// SR-8878: Using Mirror to access a weak reference results in object
103+
// being retained indefinitely
104+
mirrors.test("class/NativeSwiftClassHasNativeWeakReferenceNoLeak") {
105+
weak var verifier: AnyObject?
106+
do {
107+
let parent = NativeSwiftClassHasWeak(x: 1010)
108+
let child = NativeSwiftClass(x: 2020)
109+
verifier = child
110+
parent.weakProperty = child
111+
let mirror = Mirror(reflecting: parent)
112+
let children = Array(mirror.children)
113+
let extractedChild = children[0].1 as! NativeSwiftClass
114+
expectNotNil(extractedChild)
115+
expectNotNil(verifier)
116+
}
117+
expectNil(verifier)
118+
}
119+
102120
#if _runtime(_ObjC)
103121

104122
import Foundation

0 commit comments

Comments
 (0)