简体   繁体   中英

App crashing when writing to NSOutputStream gotten from NSNetService object

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):

0 0x35107420 in objc_msgSend ()

1 0x35750c74 in CFRelease ()

2 0x35750162 in _CFAutoreleasePoolPop ()

3 0x3093e664 in -[NSAutoreleasePool release] ()

4 0x31969130 in _UIApplicationHandleEvent ()

5 0x336adde0 in PurpleEventCallback ()

6 0x3577be46 in CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION ()

7 0x3577be04 in __CFRunLoopDoSource1 ()

8 0x3576e0a4 in __CFRunLoopRun ()

9 0x3576dd7a in CFRunLoopRunSpecific ()

10 0x3576dc88 in CFRunLoopRunInMode ()

11 0x336ace8c in GSEventRunModal ()

12 0x318f0f94 in -[UIApplication _run] ()

13 0x318ee4d4 in UIApplicationMain ()

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

Remove the [data release]; , you're creating data as autoreleased object, so you should not release it.

From stacktrace I guess the problem is that the byte array is gone - you should probably not release data at all as long as outStream is sending the data. When data is released, its byte array will vanish as well.

If it does not help, read my answer on debugging such problems here: UITextView delegates problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM