I'm having a crash but only in the ad hoc version. The debug build works fine but the adhoc crashes. This is being compiled with ARC but to use this library I have "-fno-objc-arc" set for compiler flags. Crash report is here: http://pastebin.com/edasCJbb
-(void)executePostRequestWithEndpoint:(NSString *)pathMethod usingVariables:(NSDictionary *)dict completion:(BWObjectBlock)completion failure:(BWFailureBlock)failure {
NSURL *url = [NSURL URLWithString:pathMethod];
__unsafe_unretained __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSLog(@"It gets to this NSLog and then crashes");
[request addRequestHeader:@"User-Agent" value:kMyUserAgent];
NSLog(@"It DOES NOT get to this NSLog");
NSMutableDictionary *params = dict ? [NSMutableDictionary dictionaryWithDictionary:dict] : [NSMutableDictionary dictionary];
if (params.count > 0) {
for (NSString *key in [params allKeys]) {
[request setPostValue: [[[params objectForKey:key] description] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey: key];
}
}
[request setCompletionBlock:^{
id json =[[request responseString] JSONValue];
if ([json isKindOfClass:[NSDictionary class]]) {
completion(json);
}else{
failure(json,nil);
}
}];
[request setFailedBlock:^{
id o = [[request responseString] JSONValue];
failure(o,request.error);
}];
[request startAsynchronous];
}
I fully admit that I don't really understand exactly what those compiler flags do or what the __unsafe_unratained __block does either. But I'm assuming that request is being immediately released and then bad access when used. This method is crashing on the first use in the app, but normally could get called several times in a row to access different things from my server, log in, etc. So I can't use a property for that request as it would get overwritten on the 2nd request.
Of course this is using ASIHttpRequest library from Ben Copsey which is not ARC while the rest of my app is.
Xcode 5.0 Tested and crashes on all devices from iPhone4S to 5S - but only when compiled under Archive. Debug builds don't crash. Deployment target is 5.0 but could be raised to 6.0 if it would help.
Just hoping that someone can explain this __unsafe... stuff to me and help me figure out how to fix this.
UPDATE- The crash was solved by using this answer:
ASIHTTPRequest / ASIFormDataRequest - referencing request object within blocks under ARC
But the block isn't working. Still is working different on a debug build than on a ad hoc build, which is a PITA.
I changed:
__unsafe_unretained __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
to:
ASIFormDataRequest * __weak request = [ASIFormDataRequest requestWithURL:url];
And variable is now retained long enough to prevent a crash. But what gets returned to the caller is either NULL or it never comes out of it.