这是一个非常令人困惑的错误,因为即使我发送和接收有时会发生的相同数据,有时也不会。
我通过NSInputStream接收流,我使用标准代码读取该流中收到的字节并将它们传递给另一个方法,如下所示:
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
switch (eventCode) {
case NSStreamEventHasBytesAvailable:{
NSMutableData *data = [[NSMutableData alloc] init];
uint8_t buf[1024];
NSInteger length = 0;
length = [(NSInputStream *)aStream read:buf maxLength:1024];
if(length>0) {
[data appendBytes:buf length:length];
[self.delegate doSomethingWithTheData:data];
} else {
NSLog(@"no buffer!");
}
break;
}
...
}
有时当流开始接收时,我会收到行EXC_BAD_ACCESS (code = 1, address = 0x8)
错误
length = [(NSInputStream *)aStream read:buf maxLength:1024];
我无法看到是什么导致了这个问题,因为aStream永远不会等于nil,因此在我阅读内部之前永远不会释放。我的理解是:maxLength返回一个NSInteger,然后以长度存储。我能想到的唯一问题是流中的数据,但除非我阅读它,否则我无法测试这些数据,这会导致恶性循环。
有什么想法吗?
修改
这是完整的堆栈跟踪:
(lldb) bt
* thread #1: tid = 0x2395e, 0x35b1d620 libsystem_kernel.dylib`syscall_thread_switch + 8, queue = 'com.apple.main-thread'
* frame #0: 0x35b1d620 libsystem_kernel.dylib`syscall_thread_switch + 8
frame #1: 0x35baa502 libsystem_platform.dylib`_os_lock_handoff_lock_slow + 78
frame #2: 0x35b57e14 libsystem_malloc.dylib`szone_malloc_should_clear + 56
frame #3: 0x35b5b400 libsystem_malloc.dylib`malloc_zone_calloc + 92
frame #4: 0x35b5b392 libsystem_malloc.dylib`calloc + 50
frame #5: 0x354f3410 libobjc.A.dylib`class_createInstance + 40
frame #6: 0x27e42aa0 CoreFoundation`__CFAllocateObject2 + 12
frame #7: 0x27d5b760 CoreFoundation`+[__NSArrayI __new:::] + 20
frame #8: 0x27d58d84 CoreFoundation`-[__NSPlaceholderArray initWithObjects:count:] + 136
frame #9: 0x27d5973c CoreFoundation`-[NSArray initWithArray:range:copyItems:] + 276
frame #10: 0x27d59610 CoreFoundation`+[NSArray arrayWithArray:] + 72
frame #11: 0x2b5b6bba UIKit`-[UIViewController _traitCollectionForChildEnvironment:] + 26
frame #12: 0x2b2d76d8 UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 216
frame #13: 0x2b2d758c UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 428
frame #14: 0x2b2d6458 UIKit`-[UIView(Hierarchy) removeFromSuperview] + 404
frame #15: 0x27d8d9bc CoreFoundation`-[NSArray makeObjectsPerformSelector:] + 196
frame #16: 0x2b3aa814 UIKit`-[UITableView reloadData] + 1304
frame #17: 0x0009d524 MyTestApp`-[StreamingViewController peerDisconnected](self=0x15ead880, _cmd=0x000ed1d8) + 280 at StreamingViewController.m:950
frame #18: 0x000e48e2 MyTestApp`__34-[MainViewController disconnected]_block_invoke(.block_descriptor=0x15e8f970) + 426 at MainViewController.m:1271
frame #19: 0x00259aea libdispatch.dylib`_dispatch_call_block_and_release + 10
frame #20: 0x00259ad6 libdispatch.dylib`_dispatch_client_callout + 22
frame #21: 0x0025d4f6 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 810
frame #22: 0x27e02be8 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
frame #23: 0x27e012e8 CoreFoundation`__CFRunLoopRun + 1512
frame #24: 0x27d4f620 CoreFoundation`CFRunLoopRunSpecific + 476
frame #25: 0x27d4f432 CoreFoundation`CFRunLoopRunInMode + 106
frame #26: 0x2f0fe0a8 GraphicsServices`GSEventRunModal + 136
frame #27: 0x2b33a358 UIKit`UIApplicationMain + 1440
frame #28: 0x000d18bc MyTestApp`main(argc=1, argv=0x0023aadc) + 116 at main.m:16