0
votes

In my Objective-C, iPhone App I was using NSURLSession successfully, my App started crashing with below error from iOS 12:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable dataTaskWithRequest:completionHandler:]: unrecognized selector sent to instance

To fetch Token ==>

+ (NSURLSession *)sharedSessionManager
{
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    [sessionConfiguration setHTTPAdditionalHeaders:@{
                                                  @"Accept": @"application/json"
                                                  }
         ];
session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:nil];
    });
    return session;
}

Get saved token for All future calls ==>

+ (NSURLSession *)sharedAsyncSessionManager
{
    NSString *authToken = [NSString stringWithFormat: @"Bearer %@", m_strToken];
    static NSURLSession *session = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    NSURLSessionConfiguration *sessionConfiguration =[NSURLSessionConfiguration defaultSessionConfiguration];
    [sessionConfiguration setHTTPAdditionalHeaders:@{
                                                         @"Content-Type": @"application/json",
                                                         @"Authorization": authToken}
         ];
        session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:nil];
    });
    return session;
}

Call API's ==>

NSURLSession *sessionMnger = [SessionManager sharedAsyncSessionManager];

NSURLSessionDataTask *task = [sessionMnger dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
  {
      NSMutableArray *arrTemp = [[NSMutableArray alloc]init];
  }

App crashes for dataTaskWithRequest in IOS 12. error message i can read as below:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable dataTaskWithRequest:completionHandler:]: unrecognized selector sent to instance

when ever i wanted to call API with above sessionMnger App crash....

anything missing from iOS 12 ?

1
Looks like memory management issue, try enabling "Zombie Objects" and running again to see what it reveals. (Product->Scheme-> Edit Scheme -> "Run" -> Diagnostics sub tab - Mindaugas
i did but same thing , app crash at same line . NSURLSessionDataTask *task = [sessionMnger dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) with message [__NSURLSessionLocal dataTaskWithRequest:completionHandler:]: message sent to deallocated instance 0x100c878c0 - user2813740
Ok, thats good (sort of, enabling "Zombie Objects" should not fix it, but reveal the reason for the crash). So your sessionMnger is released prematurely. I can't see how that could happen from your code. Are you sure you are not missing something relevant from your code snippets? Does those two lines (the one where you get sessionMnger and use it) follow each other without anything in between? - Mindaugas
hello, thank you so much for help i had added -fno-objc-arc (Build Phases) to SessionManager Class. once i have deleted it is retaining global session(will just down basic test working fine now.). Thank you so much. can you please add as Answer i will vote it so that it will help others. - user2813740

1 Answers

1
votes

Usually when you get "unrecognized selector sent to instance" error with unrelated class name (NSConcreteMapTable in this case) this signals memory management issue. Most likely original object is already released, memory is corrupted and runtime gets confused.

To diagnose, try enabling "Zombie Objects" from Scheme menu, that should help identify the crash cause. Look for clues why memory corruption might be happening.