2014-10-02 31 views
1

我正在使用套接字和寫入一些數據到服務器。第一次連接服務器時,一切正常。但是,當它寫入第二和第三,有時一次崩潰,出現錯誤:錯誤的對象0x7c15bad4:不正確的校驗和爲釋放的對象 - 對象可能被釋放後被修改

"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

是byteData與sharedInstance.byteData相同嗎? 如果: socket.sharedInstance.outputStream.write(data.bytes(),maxLength:data.length) – Krzysztof 2014-10-02 12:32:43

+0

那麼我應該使用data.bytes()而不是byteData? – 2014-10-02 12:34:17

+0

你可能使用byteData,但奇怪的是,在最後一行中,你只使用byteData,而在以前使用sharedInstance.byteData,不知道它是否是相同的字段? – Krzysztof 2014-10-02 12:35:25

回答

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可能無法以正確的大小進行初始化。

相關問題