I'm working on trying to understand and modify the WiTap sample iPhone application (which works fine). In my modification of the code, I'm able to get two devices to locate each other and resolve NSNetService objects, on which I then call getInputStream:outputStream
to create an NSInputStream
and an NSOutputStream
object.
The problem is that when I attempt to send something from one device to another using the NSOutputStream
, the sending application immediately crashes, even though the receiving application successfully gets the message and displays a UIAlert
.
Here is the code I'm using to send:
NSString *str = [[NSString alloc] initWithString:@"teststring7"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[str release];
[outStream write:[data bytes] maxLength:[data length]];
[data release];
I've played around with all possible combinations of releasing or not releasing or retaining or not retaining these various objects, and nothing seems to work - the sending app crashes every time.
One of the changes I made from the original sample was to make the streams properties of my app delegate like so:
@property (nonatomic, retain) NSInputStream *inStream;
@property (nonatomic, retain) NSOutputStream *outStream;
Is it possible that this is why my app crashes?
I've tried wrapping the sending code in a try/catch block, but that doesn't prevent the crash.
Update: Here is the backtrace (sorry for the font hideousness - I think that's from the pound sign in front of each line):
Thread 4 (thread 13059):
0 0x33bb32e4 in select$DARWIN_EXTSN ()
1 0x357a7426 in __CFSocketManager ()
2 0x33c14684 in _pthread_start ()
3 0x33c06014 in thread_start ()
Thread 3 (thread 12803):
0 0x33b89e70 in mach_msg_trap ()
1 0x33b8c35c in mach_msg ()
2 0x3576e7ee in __CFRunLoopServiceMachPort ()
3 0x3576dff6 in __CFRunLoopRun ()
4 0x3576dd7a in CFRunLoopRunSpecific ()
5 0x3576dc88 in CFRunLoopRunInMode ()
6 0x34177ef0 in RunWebThread ()
7 0x33c14684 in _pthread_start ()
8 0x33c06014 in thread_start ()
Thread 2 (thread 12291):
0 0x33bbe34c in kevent ()
1 0x33c8c770 in _dispatch_mgr_invoke ()
2 0x33c8c1bc in _dispatch_queue_invoke ()
3 0x33c8c35c in _dispatch_worker_thread2 ()
4 0x33c14c40 in _pthread_wqthread ()
5 0x33c0bb6c in start_wqthread ()
Thread 1 (thread 11523):