我正在尝试在TCP套接字连接中读取长字符串。
对于读取短长度的字符串,它工作正常。但是当我尝试发送长的base64编码图像时。它崩溃了,我试图增加到maxReadLength = 10000
,但仍然无法正常工作。
阅读传入消息
private func readAvailableBytes(stream: InputStream) {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: maxReadLength)
while stream.hasBytesAvailable {
let numberOfBytesRead = inputStream.read(buffer, maxLength: maxReadLength)
if numberOfBytesRead < 0 {
if let _ = inputStream.streamError {
break
}
}
❌ Crashing in below line ❌
if let reciviedMsg = String(bytesNoCopy: buffer,
length: numberOfBytesRead,
encoding: .ascii,
freeWhenDone: true)
{
delegate?.scoktDidRecivceNewMessagew(message: reciviedMsg)
}
}
}
错误
malloc: *** error for object 0x101890c00: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
答案 0 :(得分:2)
问题是缓冲区仅分配一次,但每个分配一次
String(bytesNoCopy: buffer, length: numberOfBytesRead, encoding: .ascii, freeWhenDone: true)
被调用。这是一个简短的独立示例,演示了该问题:
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 4)
memcpy(buffer, "abcd", 4)
var s = String(bytesNoCopy: buffer, length: 4, encoding: .ascii, freeWhenDone: true)
// OK
s = String(bytesNoCopy: buffer, length: 4, encoding: .ascii, freeWhenDone: true)
// malloc: *** error for object 0x101d8dc40: pointer being freed was not allocated
使用freeWhenDone: false
是解决问题的一种方法,但请注意
您最终必须释放缓冲区。
另一种方法是使用Array
(或Data
)作为缓冲区,这是自动的
函数返回时释放。示例:
var buffer = [UInt8](repeating: 0, count: maxReadLength)
while inputStream.hasBytesAvailable {
let numberOfBytesRead = inputStream.read(&buffer, maxLength: maxReadLength)
if numberOfBytesRead < 0 {
break
}
if let receivedMsg = String(bytes: buffer[..<numberOfBytesRead], encoding: .ascii) {
// ...
}
}