我正在使用套接字并将一些数据写入服务器。它第一次连接到服务器时一切正常。但当它写第二次,有时是第三次时,它会因错误而崩溃:
"malloc: *** error for object 0x7c15bad4: incorrect checksum for freed
object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug"
它在这些方面崩溃了:
var response:NSString = NSString(format: "%@",dataString)
var data:NSData = NSData(data: response.dataUsingEncoding(NSASCIIStringEncoding)!)
data.getBytes(&socket.sharedInstance.byteData)
socket.sharedInstance.outputStream.write(byteData, maxLength: data.length)
主要是在最后一行。我已经全局声明了变量。我不知道发生了什么。我添加了符号断点,但没有成功。我正在使用XCode 6.0和Swift。
进一步调查显示:
(36729,0xb0115000) malloc: protecting edges
(36729,0xb0115000) malloc: enabling scribbling to detect mods to free blocks
(36729,0xb0115000) malloc: purgeable zone does not support guard pages
(36729,0xb0115000) malloc: *** mach_vm_map(size=8388608) failed (error code=3)
*** error: can't allocate region securely
*** set a breakpoint in malloc_error_break to debug
答案 0 :(得分:2)
你可以尝试这段代码:
var response:NSString = NSString(format: "%@",dataString)
var data:NSData = NSData(data: response.dataUsingEncoding(NSASCIIStringEncoding)!)
socket.sharedInstance.outputStream.write(UnsafePointer<UInt8>(data.bytes()) , maxLength: data.length)
认为可能没有使用正确的大小初始化byteData。