0

enter image description hereI am using robbiehanson/CocoaAsyncSocket for async socket, when my iPad goes into background state i disconnect and close all the ports, but some times when iPad awakes from sleep it crashes, happened same with simulator when mac goes to sleep mode. Once i got crash log as [AsyncSocket close] unrecognized instance send to selector.

I am not able to find a solution for this, can any one help me on this. Below is my code for disconnecting.

- (void) enterBackground
{

  if (self.discoveryUdpSocket!=nil)
 {
    self.discoveryUdpSocket.delegate = nil;
    [self.discoveryUdpSocket close];
 }

  self.discoveryUdpSocket.delegate = nil;
  self.discoveryUdpSocket = nil;

}

Crash report 1

Crash report 2

Prerna chavan
  • 3,179
  • 7
  • 35
  • 77

1 Answers1

2

Have you tried to extend time when entering background mode to allow socket framework complete its job?

I mean to add in the app delegate a method:

-(void)applicationDidEnterBackground:(UIApplication *)application {
        UIApplication *app = [UIApplication sharedApplication];
        __block UIBackgroundTaskIdentifier bgTask = 0;
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            NSLog(@"Times up!");
            [application endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        }];
        // Let the async socket to complete its job and finally close the connection
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"Finishing job and closing async socket, time remaining=%f", [app backgroundTimeRemaining]);
            // Calling your background routine
            [self enterBackground];
            [app endBackgroundTask:bgTask];
            bgTask = UIBackgroundTaskInvalid;
        });
}

This is a good way to complete important job just before app is suspended. You have max 180 secs.

Artur Kucaj
  • 1,071
  • 1
  • 11
  • 16
  • Should i add this before or after the socket close method ? – Prerna chavan Jan 27 '14 at 12:55
  • Added a code that calls your method 'enterBackground'. Hope should work. – Artur Kucaj Jan 28 '14 at 09:18
  • Show a log output when app crashes – Artur Kucaj Jan 31 '14 at 10:01
  • I have added crash report to the question – Prerna chavan Jan 31 '14 at 12:28
  • @Prernachavan, thanks for report, but unfortunately it's useless, please symbolicate it, take a look i.e. here [Symbolicating an iOS crash log](https://coderwall.com/p/ezdcmg) or [Overview of iOS Crash Reporting](http://www.raywenderlich.com/33669/overview-of-ios-crash-reporting-tools-part-1) – Artur Kucaj Jan 31 '14 at 14:43
  • Hm, you said that it happens also on simulator, so try to add a breakpoint to objc_exception_throw to find out where and why exactly is exception raised. Additionally, you can use a try/catch statement. [How to add a breakpoint to objc_exception_throw?](http://stackoverflow.com/questions/1163981/how-to-add-a-breakpoint-to-objc-exception-throw) – Artur Kucaj Feb 03 '14 at 09:17