正在释放的指针未分配 [Swift]

Pointer being freed was not allocated [Swift]

我正在尝试读取 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

问题是缓冲区只分配了一次,但每次都释放

的时间
 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)作为缓冲区,这是自动的 当函数 returns 时释放。示例:

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) {
        // ...
    }
}