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):
Thread 3 (thread 12803):
Thread 2 (thread 12291):
Thread 1 (thread 11523):
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.