title: SocketStream::read crashes
body: <p>We use an in NSInputStream to receive data from an IMAP-server. We see a stange crash in Xcodes Crashes that we are not able to reproduce. The InputStream is initialized like this (i omitted some sanity-checks):</p>
  
  <pre><code>- (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,
                                         &amp;readStream,
                                         &amp;writeStream);
  
      *inputStream = (__bridge_transfer NSInputStream *)readStream;
      *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
  }
  </code></pre>
  
  <p>Later, we upgrade the streams to SSL:</p>
  
  <pre><code>[_inputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                     forKey:NSStreamSocketSecurityLevelKey];
  [_outputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                      forKey:NSStreamSocketSecurityLevelKey];
  </code></pre>
  
  <p>And we try to read data from it like this:</p>
  
  <pre><code>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 &gt; 0) {
          NSData *partialData = [NSData dataWithBytes:(const void *)readBuffer
                                               length:readBytesCount];
  }
  </code></pre>
  
  <p>This works in 99,99% of the time. But Xcodes crash reporter gives us some headache with this crash:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>This crash takes places in the line with <code>[NSData dataWithBytes:length:]</code>.
  Do you have any idea, how to provoke, debug or fix this error? </p>