Skip to content

Commit 07bb2b1

Browse files
authored
Merge pull request #41779 from DougGregor/extend-cocoaarray-buffer
2 parents 459fb37 + ea89407 commit 07bb2b1

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

stdlib/public/core/CocoaArray.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,20 +131,22 @@ internal struct _CocoaArrayWrapper: RandomAccessCollection {
131131
subRange bounds: Range<Int>,
132132
initializing target: UnsafeMutablePointer<AnyObject>
133133
) -> UnsafeMutablePointer<AnyObject> {
134-
let nsSubRange = SwiftShims._SwiftNSRange(
135-
location: bounds.lowerBound,
136-
length: bounds.upperBound - bounds.lowerBound)
137-
138-
// Copies the references out of the NSArray without retaining them
139-
core.getObjects(target, range: nsSubRange)
140-
141-
// Make another pass to retain the copied objects
142-
var result = target
143-
for _ in bounds {
144-
result.initialize(to: result.pointee)
145-
result += 1
134+
return withExtendedLifetime(buffer) {
135+
let nsSubRange = SwiftShims._SwiftNSRange(
136+
location: bounds.lowerBound,
137+
length: bounds.upperBound - bounds.lowerBound)
138+
139+
// Copies the references out of the NSArray without retaining them
140+
core.getObjects(target, range: nsSubRange)
141+
142+
// Make another pass to retain the copied objects
143+
var result = target
144+
for _ in bounds {
145+
result.initialize(to: result.pointee)
146+
result += 1
147+
}
148+
return result
146149
}
147-
return result
148150
}
149151

150152
@_alwaysEmitIntoClient

0 commit comments

Comments
 (0)