Skip to content

Commit f601b0f

Browse files
fix deallocation - use DispatchData(bytesNoCopy with deallocator)
1 parent 9b7dd7d commit f601b0f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Foundation/URLSession/BodySource.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,20 @@ extension _BodyStreamSource : _BodySource {
7474

7575
let buffer = UnsafeMutableRawBufferPointer.allocate(count: length)
7676
guard let pointer = buffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else {
77+
buffer.deallocate()
7778
return .error
7879
}
7980
let readBytes = self.inputStream.read(pointer, maxLength: length)
8081
if readBytes > 0 {
81-
let dispatchData = DispatchData(bytes: UnsafeRawBufferPointer(buffer))
82+
let dispatchData = DispatchData(bytesNoCopy: UnsafeRawBufferPointer(buffer), deallocator: .free)
8283
return .data(dispatchData.subdata(in: 0 ..< readBytes))
8384
}
8485
else if readBytes == 0 {
86+
buffer.deallocate()
8587
return .done
8688
}
8789
else {
90+
buffer.deallocate()
8891
return .error
8992
}
9093
}

0 commit comments

Comments
 (0)