xcode NSStreams崩溃程序!

omjgkv6w  于 2023-08-07  发布在  其他
关注(0)|答案(6)|浏览(115)

所有人
我已经通过注解、断点等将它运行到这一点。程序在标记的代码处崩溃。

-(void) initNetworkCommunication
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.17.1", 2004, &readStream, &writeStream);

    inputStream = (NSInputStream *)readStream;
    outputStream = (NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];//WHY MUST YOU CRASH HERE
    [outputStream open];//WHY MUST YOU CRASH HERE ALSO!!?!?

    NSLog(@"She be opened, sir!");
}

字符串
如果我注解掉这两个,它不会崩溃,但是如果我注解掉其中一个,它会崩溃(所以,即它们都导致程序崩溃)。调试器中也没有发布任何信息。它所做的就是把我发送到main.m并显示给我
“线程1:程序接收信号:“EXC_BAD_ACCESS”.
提前感谢您的帮助!

编辑:这里是我的delegate方法,但它甚至没有在日志中显示第二个活动行。

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

    NSLog(@"stream event %i", streamEvent); //this doesn't post in the log when stream opened...

    switch (streamEvent) {

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            break;
        case NSStreamEventHasBytesAvailable:

            if (theStream == inputStream) {

                uint8_t buffer[1024];
                int len;

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        if (nil != output) {

                            NSLog(@"server said: %@", output);
                            //[self messageReceived:output];

                        }
                    }
                }
            }
            break;

        case NSStreamEventErrorOccurred:

            NSLog(@"Can not connect to the host!");
            break;

        case NSStreamEventEndEncountered:

            [theStream close];
            [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            //[theStream release];
            theStream = nil;

            break;
        default:
            NSLog(@"Unknown event");
    }

}

zf9nrax1

zf9nrax11#

发生的情况是,委托类的示例正在被释放(导致运行循环中的EXC_BAD_ACCESS),这可能是因为您没有保留它,或者您正在使用ARC(很可能)并且您没有对它的引用。
解决方案是在delegate类上调用retain,大致如下:

SomeStreamDelegate *theDelegate = [[SomeStreamDelegate alloc] init];
[theDelegate retain];

字符串
或者,如果您启用了ARC,请在分配委托的类中创建一个示例变量,并将连接示例存储在那里。这样ARC就不会释放它,因为示例变量被视为引用。

nhn9ugyo

nhn9ugyo2#

如果你使用的是ARC,请像这样投射流:

inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;

字符串
这应该可以防止崩溃。请记住,如果您的流由一个独立的线程而不是主线程拥有,这意味着需要在打开流后使用run方法手动调用run循环。

vmdwslir

vmdwslir3#

当我把它放在我的视图控制器(而不是一个单独的类)中时,它工作得很好。

ibps3vxo

ibps3vxo4#

我遇到过类似的问题,我的应用程序会在-handleEvent回调中崩溃,并带有一个巨大的streamEvent数字。我通过确保初始化NSStream对象(输入和输出)在我的VC计划使用的NetworkClient对象的-init方法中打开到服务器的连接来解决它。

06odsfpq

06odsfpq5#

我遇到了类似的问题,并获得了以下堆栈跟踪:

Crashed: [connection thread]
0  libobjc.A.dylib                0x1c08 objc_msgSend + 8
1  T4Mobile                       0x446a4 -[SSLConnection onBytesAvailable] + 573 (SSLConnection.m:573)
2  T4Mobile                       0x43188 -[SSLConnection stream:handleEvent:] + 235 (SSLConnection.m:235)
3  CoreFoundation                 0x105fb4 _signalEventSync + 216
4  CoreFoundation                 0x117e84 _cfstream_solo_signalEventSync + 224
5  CoreFoundation                 0x10e42c _CFStreamSignalEvent + 304
6  CFNetwork                      0x147eb8 _CFNetworkErrorGetLocalizedDescription + 180236
7  CFNetwork                      0x1533e4 _CFNetworkErrorGetLocalizedDescription + 226616
8  CoreFoundation                 0x11a648 __CFSocketPerformV0 + 1204
9  CoreFoundation                 0xd5f54 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
10 CoreFoundation                 0xe232c __CFRunLoopDoSource0 + 176
11 CoreFoundation                 0x66210 __CFRunLoopDoSources0 + 244
12 CoreFoundation                 0x7bba8 __CFRunLoopRun + 836
13 CoreFoundation                 0x80ed4 CFRunLoopRunSpecific + 612
14 Foundation                     0x42334 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212
15 T4Mobile                       0xc1128 -[iOSHostConnectionOC connectionThreadManin:] + 344 (iOSHostConnectionOC.m:344)
16 Foundation                     0x5b808 __NSThread__start__ + 716
17 libsystem_pthread.dylib        0x16cc _pthread_start + 148
18 libsystem_pthread.dylib        0xba4 thread_start + 8

字符串
访问inputStream时发生崩溃。

while([_inputStream hasBytesAvailable]) {
    NSInteger len = [_inputStream read:_readBuffer maxLength:4096];

2nc8po8w

2nc8po8w6#

试一次
NSInputStream * inputStream = objc_unretainedObject(readStream);
May be a casting issue

相关问题