Skip to content

Commit 4f67097

Browse files
fix deallocation - use buffer.deallocate
1 parent f601b0f commit 4f67097

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Foundation/URLSession/BodySource.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,25 @@ extension _BodyStreamSource : _BodySource {
7070
guard inputStream.hasBytesAvailable else {
7171
return .done
7272
}
73-
7473

7574
let buffer = UnsafeMutableRawBufferPointer.allocate(count: length)
76-
guard let pointer = buffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
75+
defer {
7776
buffer.deallocate()
77+
}
78+
79+
guard let pointer = buffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
7880
return .error
7981
}
82+
8083
let readBytes = self.inputStream.read(pointer, maxLength: length)
8184
if readBytes > 0 {
82-
let dispatchData = DispatchData(bytesNoCopy: UnsafeRawBufferPointer(buffer), deallocator: .free)
85+
let dispatchData = DispatchData(bytes: UnsafeRawBufferPointer(buffer))
8386
return .data(dispatchData.subdata(in: 0 ..< readBytes))
8487
}
8588
else if readBytes == 0 {
86-
buffer.deallocate()
8789
return .done
8890
}
8991
else {
90-
buffer.deallocate()
9192
return .error
9293
}
9394
}

0 commit comments

Comments
 (0)