hakansaglam
hakansaglam

Reputation: 100

iOS Socket Connection Crashes

I have created socket connection mechanism in iOS. It is working quite well. But from time to time (around 1%) it is crashing. Here it is the crash log, do you have any idea why it is happening.

Thread 0 Crashed:
0   libobjc.A.dylib                     0x3b01b5b0 _objc_msgSend + 16
1   CoreFoundation                      0x332957cf _signalEventSync + 75
2   CoreFoundation                      0x3329b623 _cfstream_solo_signalEventSync + 75
3   CoreFoundation                      0x33295507 _CFStreamSignalEvent + 327
4   CFNetwork                           0x32ffa6ff CoreWriteStreamCFStreamSupport::coreStreamWriteEvent(__CoreWriteStream*, unsigned long) + 99
5   CFNetwork                           0x32ffa0b5 CoreWriteStreamClient::coreStreamEventsAvailable(unsigned long) + 37
6   CFNetwork                           0x32ffb365 CoreStreamBase::_callClientNow() + 45
7   CFNetwork                           0x32ffb0f9 CoreStreamBase::_streamSetEventAndScheduleDelivery(unsigned long, unsigned char) + 89
8   CFNetwork                           0x32ffb4ff CoreStreamBase::_streamInterface_SignalEvent(unsigned long, CFStreamError const*) + 35
9   CFNetwork                           0x32f69b57 SocketStream::socketCallback(__CFSocket*, unsigned long, __CFData const*, void const*) + 135
10  CFNetwork                           0x32f69ab3 SocketStream::_SocketCallBack_stream(__CFSocket*, unsigned long, __CFData const*, void const*, void*) + 75
11  CoreFoundation                      0x332cfd81 __CFSocketPerformV0 + 385
12  CoreFoundation                      0x332cd683 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
13  CoreFoundation                      0x332ccf7f __CFRunLoopDoSources0 + 363
14  CoreFoundation                      0x332cbcb7 __CFRunLoopRun + 647
15  CoreFoundation                      0x3323eebd _CFRunLoopRunSpecific + 357
16  CoreFoundation                      0x3323ed49 _CFRunLoopRunInMode + 105
17  GraphicsServices                    0x36e172eb _GSEventRunModal + 75
18  UIKit                               0x35154301 _UIApplicationMain + 1121
19  Okey101Plus                         0x000e19ab main (main.m:16)

Upvotes: 0

Views: 1506

Answers (2)

dgatwood
dgatwood

Reputation: 10417

It looks like the socket class is trying to send a message to your delegate, and the delegate has become a zombie.

The NSStream class does not retain its delegate, unlike most other networking classes in iOS/OS X, so if you don't retain it somewhere, it will go away. Then, when the class tries to tell your class that the stream has data for reading or space for writing, you'll get a crash in objc_msgSend, like this one.

And if you do retain it, make sure you fully tear down the stream before you get rid of that retained delegate, and be careful about what thread you tear things down on, to ensure that there aren't callbacks already scheduled on the main run loop that will fire after you release the delegate.

Either that or you failed to implement a required delegate method. However, that doesn't fit with the "occurs rarely" bit. :-)

Upvotes: 2

ambientlight
ambientlight

Reputation: 7332

I'm not really really sure, but based on your report it looks like writing on socket fails -

CoreWriteStreamCFStreamSupport::coreStreamWriteEvent(__CoreWriteStream*, unsigned long) + 99 

there is probably a way how you can avoid this error. CFNetworks Framework has a nice feature called CFNetDiagnostics reference. Please take a closer look at apple docs for it.

From Apple Docs: In many network-based applications, network-based errors may occur that are unrelated to your application.(They probably mean here, for example: you can get reading error on socket, if the interface becomes unavailable, when network connection is lost) However, most users are probably unaware of why an application is failing. The CFNetDiagnostics API allows you a quick and easy way to help the user fix their network problems with little work on your end.

To diagnose the problem through the Network Diagnostic Assistant, call the CFNetDiagnosticDiagnoseProblemInteractively function and pass the network diagnostic reference. Listing 6-1 shows how to use CFNetDiagnostics with streams implemented on a run loop.

case kCFStreamEventErrorOccurred:
        CFNetDiagnosticRef diagRef =
            CFNetDiagnosticCreateWithStreams(NULL, stream, NULL);
        (void)CFNetDiagnosticDiagnoseProblemInteractively(diagRef);
        CFStreamError error = CFReadStreamGetError(stream);
        reportError(error);
        CFReadStreamClose(stream);
        CFRelease(stream);
        break;

You can read more here - Using Network Diagnostics, CFNetDiagnostics Reference

Upvotes: 1

Related Questions