Skip to content

Extend the lifetime of the buffer while copying from a _CocoaArrayWrapper #41779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions stdlib/public/core/CocoaArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,22 @@ internal struct _CocoaArrayWrapper: RandomAccessCollection {
subRange bounds: Range<Int>,
initializing target: UnsafeMutablePointer<AnyObject>
) -> UnsafeMutablePointer<AnyObject> {
let nsSubRange = SwiftShims._SwiftNSRange(
location: bounds.lowerBound,
length: bounds.upperBound - bounds.lowerBound)

// Copies the references out of the NSArray without retaining them
core.getObjects(target, range: nsSubRange)

// Make another pass to retain the copied objects
var result = target
for _ in bounds {
result.initialize(to: result.pointee)
result += 1
return withExtendedLifetime(buffer) {
let nsSubRange = SwiftShims._SwiftNSRange(
location: bounds.lowerBound,
length: bounds.upperBound - bounds.lowerBound)

// Copies the references out of the NSArray without retaining them
core.getObjects(target, range: nsSubRange)

// Make another pass to retain the copied objects
var result = target
for _ in bounds {
result.initialize(to: result.pointee)
result += 1
}
return result
}
return result
}

@_alwaysEmitIntoClient
Expand Down