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.

Answers


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.


Need Your Help

Running out of memory when trying to perform task with apply rather than for loop in R

r memory apply transpose

I'm trying to rewrite some old code in order for it to be more efficient. I read in my places that using apply should be faster than using a for loop, so I attempted to do this. First the old working

Visual Studio 2005 - can not open form on designer

visual-studio-2005 windows-forms-designer

My team developed a GUI application on Visual Studio 2005, managed C++. Since some deliveries it is not possible to open the form in the designer, even if the source code and the project settings h...