This is a very confusing error, as even if I send and receive the same data it sometimes happens, sometimes doesn't.
I am receiving a stream via NSInputStream and I use standard code to read the bytes received in that stream and pass them onto another method, like this:
- (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;
}
...
}
Sometimes when the stream starts being received I get an EXC_BAD_ACCESS (code = 1, address = 0x8)
error for the line
length = [(NSInputStream *)aStream read:buf maxLength:1024];
I can't see what is causing this issue as aStream is never equal to nil and therefore is never deallocated before I read what's inside. It is my understanding that read: maxLength returns an NSInteger which would then be stored in length. The only issue I can think of would be with the data in the stream but I have no way to test this data unless I read it, which then leads to a vicious circle.
Any thoughts?
EDIT
This is the complete stack trace:
(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