我'm working on trying to understand and modify the WiTap sample iPhone application (which works fine). In my modification of the code, I'能够让两个设备相互定位并解析NSNetService对象,然后我在其上调用 getInputStream:outputStream
以创建 NSInputStream
和 NSOutputStream
对象 .
问题是当我尝试使用 NSOutputStream
从一个设备向另一个设备发送内容时,即使接收应用程序成功获取消息并显示 UIAlert
,发送应用程序也会立即崩溃 .
这是我用来发送的代码:
NSString *str = [[NSString alloc] initWithString:@"teststring7"];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[str release];
[outStream write:[data bytes] maxLength:[data length]];
[data release];
我已经玩过所有可能的组合释放或不释放或保留或不保留这些不同的对象,似乎没有任何工作 - 发送应用程序每次都崩溃 .
我从原始示例中做出的一项更改是使我的app委托的流属性如下:
@property (nonatomic, retain) NSInputStream *inStream;
@property (nonatomic, retain) NSOutputStream *outStream;
这可能是我的应用程序崩溃的原因吗?
我尝试在try / catch块中包装发送代码,但这并不能防止崩溃 .
Update: 这是回溯(抱歉字体丑陋 - 我认为这是来自每行前面的英镑符号):
线程4(线程13059):
选择$ DARWIN_EXTSN()中
0 0x33bb32e4
__CFSocketManager()中
1 0x357a7426
_pthread_start()中
2 0x33c14684
thread_start()中的
3 0x33c06014
线程3(线程12803):
mach_msg_trap()中
0 0x33b89e70
mach_msg中的
1 0x33b8c35c()
__CFRunLoopServiceMachPort()中
2 0x3576e7ee
__CFRunLoopRun()中
3 0x3576dff6
CFRunLoopRunSpecific()中的
4 0x3576dd7a
CFRunLoopRunInMode()中的
5 0x3576dc88
RunWebThread()中
6 0x34177ef0
_pthread_start()中
7 0x33c14684
thread_start()中的
8 0x33c06014
线程2(线程12291):
kevent()中
0 0x33bbe34c
_dispatch_mgr_invoke()中
1 0x33c8c770
_dispatch_queue_invoke()中
2 0x33c8c1bc
_dispatch_worker_thread2()中
3 0x33c8c35c
_pthread_wqthread()中
4 0x33c14c40
start_wqthread()中
5 0x33c0bb6c
线程1(线程11523):
objc_msgSend()中
0 0x35107420
CFRelease中的
1 0x35750c74()
_CFAutoreleasePoolPop()中
2 0x35750162
3 0x3093e664 in - [NSAutoreleasePool release]()
_UIApplicationHandleEvent()中的
4 0x31969130
PurpleEventCallback()中
5 0x336adde0
CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION()中的
6 0x3577be46
__CFRunLoopDoSource1()中
7 0x3577be04
__CFRunLoopRun()中
8 0x3576e0a4
CFRunLoopRunSpecific()中的
9 0x3576dd7a
CFRunLoopRunInMode()中的
10 0x3576dc88
GSEventRunModal()中的
11 0x336ace8c
12 0x318f0f94 in - [UIApplication _run]()
UIApplicationMain()中
13 0x318ee4d4
main中的
1 回答
删除
[data release];
,您正在创建data
作为自动释放的对象,因此您不应该释放它 .从堆栈跟踪我想问题是字节数组已经消失 - 只要
outStream
正在发送数据,你根本不应该释放data
. 释放data
时,其字节数组也将消失 .如果它没有帮助,请在此处阅读我关于调试此类问题的答案:UITextView delegates problem