未分配要释放的指针[Swift]

时间:2018-10-08 19:04:19

标签: swift tcp tcpclient

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

1 个答案:

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