Andrew Rangel
Andrew Rangel

Reputation: 23

EXC_BAD_ACCESS when doing a POST using AFNetworking 2.0

My code:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager] ;
manager.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:[_urlBase stringByAppendingPathComponent:_urlRequest]
         parameters:paramDictionary
         success:^(NSURLSessionDataTask *task, id responseObject){
             dispatch_async(dispatch_get_main_queue(),^{
                 [self AFRequestFinished:responseObject];
             });
         }
         failure:^(NSURLSessionDataTask *task, NSError *error){
             NSLog(@"JSON ERROR PARAMETERS: %@", error);
         }
];

I am using this POST request to send several types of data up to a server along with pictures.
I am using something very similar for the GET request and it works fine. Whenever I run this code I get a EXC_BAD_ACCESS CODE=1 error on the following line of AFNetworking 2.0. The responseObject is 0x0:

responseObject = [self.responseSerializer responseObjectForResponse:task.response data:[NSData dataWithData:self.mutableData] error:&serializationError];

The above line of code is within the if/else method in:

- (void)URLSession:(__unused NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error

UPDATE

I ran instruments on the code, and it there is a zombie present. AFNetworking is trying to make a call to the NSError, but it has been deallocated. I believe this has arisen because the POST call initially succeeds, but there is still an error that is flagged. So it initially thinks there is no error and sets it to nil, but then tries to call for it in the error block of the POST.

Upvotes: 2

Views: 822

Answers (1)

Aaron Brager
Aaron Brager

Reputation: 66244

If you're using the most recent version, you may be experiencing this known issue when the JSON serializer returns an error. You can work around this until a new release is made by:

  • removing the @autoreleasepool in the serializer, or
  • changing the scope of the error to outside the autorelease pool

(Both solutions are outlined in the issue linked above.)

On a side note, there's no need to dispatch to the main queue in the completion handler. AFNetworking guarantees that completion blocks are called on the main thread.

Upvotes: 1

Related Questions