Skip to content

Commit a026128

Browse files
committed
TestFoundation: ensure that NSValue is preserved
Extend the lifetime of the `NSValue` object as we retrieve a pointer to the data owned by it. Since the data is owned by the `NSValue` instance, we need to ensure that the `NSValue` has its lifetime extended throughout the usage.
1 parent 120f749 commit a026128

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

TestFoundation/TestNSKeyedArchiver.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,16 @@ class TestNSKeyedArchiver : XCTestCase {
289289
guard let value = unarchiver.decodeObject(of: NSValue.self, forKey: "root") else {
290290
return false
291291
}
292-
var expectedCharPtr: UnsafeMutablePointer<CChar>? = nil
293-
value.getValue(&expectedCharPtr)
294-
295-
let s1 = String(cString: charPtr)
296-
let s2 = String(cString: expectedCharPtr!)
297292

298-
return s1 == s2
293+
return withExtendedLifetime(value) { value in
294+
var expectedCharPtr: UnsafeMutablePointer<CChar>? = nil
295+
value.getValue(&expectedCharPtr)
296+
297+
let s1 = String(cString: charPtr)
298+
let s2 = String(cString: expectedCharPtr!)
299+
300+
return s1 == s2
301+
}
299302
})
300303
}
301304

0 commit comments

Comments
 (0)