【发布时间】:2014-10-02 12:14:52
【问题描述】:
我正在使用套接字并将一些数据写入服务器。第一次连接到服务器时一切正常。但是当它写第二次,有时是第三次时,它会因错误而崩溃:
"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
【问题讨论】:
-
byteData 和 sharedInstance.byteData 一样吗?如果:socket.sharedInstance.outputStream.write(data.bytes(), maxLength: data.length)
-
那么我应该使用 data.bytes() 而不是 byteData 吗?
-
不,你可能会使用 byteData,但奇怪的是在最后一行你只使用了 byteData 而在之前你使用 sharedInstance.byteData,不确定它是否是同一个字段?
-
是的,我已经尝试了这两种方法,但同样的事情发生了。
-
如果你删除第 3 行,并使用 data.bytes() 它仍然崩溃吗?指针有问题,甚至可能不在您发布的这段代码中,而是在其他线程中。