0

我们在 NSInputStream 中使用一个从 IMAP 服务器接收数据。我们在 Xcodes 崩溃中看到了一个无法重现的异常崩溃。InputStream 是这样初始化的(我省略了一些完整性检查):

- (void)getStreamsToServer:(NSString *)hostName
                   andPort:(NSUInteger)port
            andInputStream:(NSInputStream **)inputStream
           andOutputStream:(NSOutputStream **)outputStream
{
    CFReadStreamRef readStream = NULL;
    CFWriteStreamRef writeStream = NULL;
    CFStreamCreatePairWithSocketToHost(NULL,
                                       (__bridge CFStringRef)(hostName),
                                       (unsigned int)port,
                                       &readStream,
                                       &writeStream);

    *inputStream = (__bridge_transfer NSInputStream *)readStream;
    *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
}

稍后,我们将流升级到 SSL:

[_inputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                   forKey:NSStreamSocketSecurityLevelKey];
[_outputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                    forKey:NSStreamSocketSecurityLevelKey];

我们尝试像这样从中读取数据:

NSUInteger READ_BUFFER_LENGTH = 16 * 1024;
uint8_t readBuffer[READ_BUFFER_LENGTH];
NSInteger readBytesCount = 0;
if([_inputStream hasBytesAvailable]) {
    readBytesCount = [_inputStream read:readBuffer
                              maxLength:READ_BUFFER_LENGTH];
    if (readBytesCount > 0) {
        NSData *partialData = [NSData dataWithBytes:(const void *)readBuffer
                                             length:readBytesCount];
}

这在 99,99% 的时间内有效。但是 Xcodes 崩溃报告器让我们对这次崩溃有些头疼:

0   CoreFoundation                  0x0000000184970620 CFHash + 360 (CFRuntime.c:1080)
1   CoreFoundation                  0x00000001849718b8 CFBasicHashGetCountOfKey + 160 (CFBasicHash.c:455)
2   CoreFoundation                  0x00000001849718b8 CFBasicHashGetCountOfKey + 160 (CFBasicHash.c:455)
3   CoreFoundation                  0x0000000184971804 CFSetContainsValue + 208 (CFSet.c:405)
4   CoreFoundation                  0x00000001849a0148 CFRunLoopRemoveSource + 164 (CFRunLoop.c:3542)
5   CFNetwork                       0x0000000185113e78 SocketStream::read(__CFReadStream*, unsigned char*, long, CFStreamError*, unsigned char*) + 604 (SocketStream.cpp:2757)
6   CoreFoundation                  0x000000018499c7c8 CFReadStreamRead + 480 (CFStream.c:1146)

这次崩溃发生在符合[NSData dataWithBytes:length:]. 你有什么想法,如何引发、调试或修复这个错误?

4

1 回答 1

0

尝试使用 SocketIO

socket.io-client-swift

于 2017-12-14T12:21:10.033 回答