5

I'm writing an app which uses NSOutputStream. I init connection like this:

delegate = self;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;

CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)url,port, &readStream, &writeStream);
CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

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

[inputStream setDelegate:delegate];
[outputStream setDelegate:delegate];

loop = [NSRunLoop currentRunLoop];
[inputStream scheduleInRunLoop:loop forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:loop forMode:NSDefaultRunLoopMode];

[inputStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];
[outputStream setProperty:NSStreamSocketSecurityLevelTLSv1 forKey:NSStreamSocketSecurityLevelKey];



[inputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                  forKey:NSStreamSocketSecurityLevelKey];
[outputStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                   forKey:NSStreamSocketSecurityLevelKey];

NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES], kCFStreamSSLAllowsExpiredCertificates,
                          [NSNumber numberWithBool:YES], kCFStreamSSLAllowsAnyRoot,
                          [NSNumber numberWithBool:NO], kCFStreamSSLValidatesCertificateChain,
                          kCFNull,kCFStreamSSLPeerName,
                          nil];

CFReadStreamSetProperty((CFReadStreamRef)inputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
CFWriteStreamSetProperty((CFWriteStreamRef)outputStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);

[outputStream open];
[inputStream open];

[self sendVersionOrWait];
[loop run];

and then do some action depends on NSStreamDelegate methods. Closing conection is done by:

 [inputStream close];
[outputStream close];
[inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream setDelegate:nil];
[outputStream setDelegate:nil];

inputStream = nil;
outputStream = nil;

Sometimes (it is randomly) I get EXC_BREAKPOINT and the app crashes. This is part of crash log:

Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x000000000000defe Triggered by Thread: 5

Thread 5 name: Dispatch queue: com.apple.root.default-qos

Thread 5 Crashed:

0 CoreFoundation 0x22767e22 CFHash + 130

1 CoreFoundation 0x22768d70 CFBasicHashGetCountOfKey + 1152

2 CoreFoundation 0x227688aa CFSetContainsValue + 98

3 CoreFoundation 0x2279ca5a CFRunLoopRemoveSource + 226

4 CFNetwork 0x22343eca SocketStream::write(__CFWriteStream*, unsigned char const*, long, CFStreamError*) + 426

5 CFNetwork 0x223480c2 WriteStreamCallbacks::_write(__CFWriteStream*, unsigned char const*, long, CFStreamError*, void*) + 34

6 CoreFoundation 0x2278d7ec CFWriteStreamWrite + 356

7 App 0x000a0190 -[AppMenuViewController sendVersion] (AppMenuViewController.m:763)

8 App 0x000a0c68 -[AppMenuViewController sendVersionOrWait] (AppMenuViewController.m:832)

9 Foundation 0x235655e4 __NSFireDelayedPerform + 464

10 CoreFoundation 0x22828734 CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 12

11 CoreFoundation 0x228282b4 __CFRunLoopDoTimer + 652

12 CoreFoundation 0x2282651e __CFRunLoopRun + 1414

13 CoreFoundation 0x22773dac CFRunLoopRunSpecific + 472

14 CoreFoundation 0x22773bbe CFRunLoopRunInMode + 102

15 Foundation 0x234ab16c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 260

16 Foundation 0x234f95e0 -[NSRunLoop(NSRunLoop) run] + 76

17 App 0x0009feaa -[AppMenuViewController initConnection:withPort:] (AppMenuViewController.m:749)

18 App 0x000a1642 -[AppMenuViewController stream:handleEvent:] (AppMenuViewController.m:952)

19 CoreFoundation 0x227d9b94 _signalEventSync + 144

20 CoreFoundation 0x227e3ef2 _cfstream_solo_signalEventSync + 198

21 CoreFoundation 0x227d981e _CFStreamSignalEvent + 322

22 CFNetwork 0x222b7bd4 SocketStream::dispatchSignalFromSocketCallbackUnlocked(SocketStreamSignalHolder*) + 36

23 CFNetwork 0x222b78be SocketStream::socketCallback(__CFSocket*, unsigned long, __CFData const*, void const*) + 146

24 CFNetwork 0x222b77f2 SocketStream::_SocketCallBack_stream(__CFSocket*, unsigned long, __CFData const*, void const*, void*) + 54

25 CoreFoundation 0x2282b0bc __CFSocketPerformV0 + 552

26 CoreFoundation 0x22828804 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 12

27 CoreFoundation 0x22827c16 __CFRunLoopDoSources0 + 218

28 CoreFoundation 0x22826294 __CFRunLoopRun + 764

29 CoreFoundation 0x22773dac CFRunLoopRunSpecific + 472

30 CoreFoundation 0x22773bbe CFRunLoopRunInMode + 102

31 Foundation 0x234ab16c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 260

32 Foundation 0x234f95e0 -[NSRunLoop(NSRunLoop) run] + 76

33 App 0x0009feaa -[AppMenuViewController initConnection:withPort:] (AppMenuViewController.m:749)

34 App 0x0009b664 __58-[AppMenuViewController alertView:clickedButtonAtIndex:]_block_invoke (AppMenuViewController.m:385)

35 libdispatch.dylib 0x30521610 _dispatch_call_block_and_release + 8

36 libdispatch.dylib 0x3052d350 _dispatch_root_queue_drain + 816

37 libdispatch.dylib 0x3052e27a _dispatch_worker_thread3 + 102

38 libsystem_pthread.dylib 0x3069ee22 _pthread_wqthread + 666

39 libsystem_pthread.dylib 0x3069eb74 start_wqthread + 4

I suppose it can be caused by network problems because it appears when my WIFI router doesn't have Internet connection. But I'm not sure and I don't know how to remove this error. Please, help :)

ibrig
  • 51
  • 1

1 Answers1

0

My guess is that this is related to threading. Try doing a dispatch to main thread to make sure this is all done on the main thread, and see if that magically cures the problem. If so, you know where to look....

xaphod
  • 6,392
  • 2
  • 37
  • 45