App crashing when writing to NSOutputStream gotten from NSNetService object
<p>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 <code>getInputStream:outputStream</code> to create an <code>NSInputStream</code> and an <code>NSOutputStream</code> object.</p>

<p>The problem is that when I attempt to send something from one device to another using the <code>NSOutputStream</code>, the sending application immediately crashes, even though the receiving application successfully gets the message and displays a <code>UIAlert</code>.</p>

<p>Here is the code I'm using to send:</p>

<pre><code>NSString *str = [[NSString alloc] initWithString:@"teststring7"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[str release];
[outStream write:[data bytes] maxLength:[data length]];
[data release];
</code></pre>

<p>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.</p>

<p>One of the changes I made from the original sample was to make the streams properties of my app delegate like so:</p>

<pre><code>@property (nonatomic, retain) NSInputStream *inStream;
@property (nonatomic, retain) NSOutputStream *outStream;
</code></pre>

<p>Is it possible that this is why my app crashes?</p>

<p>I've tried wrapping the sending code in a try/catch block, but that doesn't prevent the crash.</p>

<p><strong>Update:</strong>  Here is the backtrace (sorry for the font hideousness - I think that's from the pound sign in front of each line):</p>

<p>Thread 4 (thread 13059):</p>

<h1>0  0x33bb32e4 in select$DARWIN_EXTSN ()</h1>

<h1>1  0x357a7426 in __CFSocketManager ()</h1>

<h1>2  0x33c14684 in _pthread_start ()</h1>

<h1>3  0x33c06014 in thread_start ()</h1>

<p>Thread 3 (thread 12803):</p>

<h1>0  0x33b89e70 in mach_msg_trap ()</h1>

<h1>1  0x33b8c35c in mach_msg ()</h1>

<h1>2  0x3576e7ee in __CFRunLoopServiceMachPort ()</h1>

<h1>3  0x3576dff6 in __CFRunLoopRun ()</h1>

<h1>4  0x3576dd7a in CFRunLoopRunSpecific ()</h1>

<h1>5  0x3576dc88 in CFRunLoopRunInMode ()</h1>

<h1>6  0x34177ef0 in RunWebThread ()</h1>

<h1>7  0x33c14684 in _pthread_start ()</h1>

<h1>8  0x33c06014 in thread_start ()</h1>

<p>Thread 2 (thread 12291):</p>

<h1>0  0x33bbe34c in kevent ()</h1>

<h1>1  0x33c8c770 in _dispatch_mgr_invoke ()</h1>

<h1>2  0x33c8c1bc in _dispatch_queue_invoke ()</h1>

<h1>3  0x33c8c35c in _dispatch_worker_thread2 ()</h1>

<h1>4  0x33c14c40 in _pthread_wqthread ()</h1>

<h1>5  0x33c0bb6c in start_wqthread ()</h1>

<p>Thread 1 (thread 11523):</p>

<h1>0  0x35107420 in objc_msgSend ()</h1>

<h1>1  0x35750c74 in CFRelease ()</h1>

<h1>2  0x35750162 in _CFAutoreleasePoolPop ()</h1>

<h1>3  0x3093e664 in -[NSAutoreleasePool release] ()</h1>

<h1>4  0x31969130 in _UIApplicationHandleEvent ()</h1>

<h1>5  0x336adde0 in PurpleEventCallback ()</h1>

<h1>6  0x3577be46 in <strong>CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION</strong> ()</h1>

<h1>7  0x3577be04 in __CFRunLoopDoSource1 ()</h1>

<h1>8  0x3576e0a4 in __CFRunLoopRun ()</h1>

<h1>9  0x3576dd7a in CFRunLoopRunSpecific ()</h1>

<h1>10 0x3576dc88 in CFRunLoopRunInMode ()</h1>

<h1>11 0x336ace8c in GSEventRunModal ()</h1>

<h1>12 0x318f0f94 in -[UIApplication _run] ()</h1>

<h1>13 0x318ee4d4 in UIApplicationMain ()</h1>

<h1>14 0x00002444 in main (argc=1, argv=0x2ffff5c0) at /Users/kenadams/Documents/WalkyTalkyX/main.m:14</h1>
