1

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.

Community
  • 1
  • 1
badweasel
  • 2,349
  • 1
  • 19
  • 31
  • Why are you using `__unsafe_unretained` and `__block` qualifiers - what problem are you trying to solve there? Have you read Apple's description in [the ARC transition notes](https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW4), under the heading "Variable Qualifiers"? – Carl Veazey Nov 07 '13 at 01:47
  • I think an employee originally wrote that code. Cause I would never do that on my own. As I said I don't know what that part does. Should I just remove that qualifier? No I have not read it but thanks for the link. – badweasel Nov 07 '13 at 03:30
  • I would try removing both those first - since you reference the request in the blocks, those should keep a strong reference to it and you wouldn't necessarily need a property. – Carl Veazey Nov 07 '13 at 03:32
  • When I remove the __unsafe_unretained I get a compiler warning: "Capturing 'request' strongly in this block is likely to lead to a retain cycle" – badweasel Nov 07 '13 at 06:49
  • Does it fix the crash? – Carl Veazey Nov 07 '13 at 07:25
  • Just removing them would fix the crash but probably cause a memory leak. According to your linked apple doc, then it would default to __strong. What fixed the crash was replacing __unsafe_unretained with __weak (I also removed __block), which is a temporary variable. I found another guy who had the same issue where it only crashes in the ad hoc. And he also fixed it by changing it to __weak. I did it as an update here as my question is clearly a duplicate at this point. But you did help me find the answer. Thanks for your comments. – badweasel Nov 07 '13 at 07:28
  • not so fast. Checking that possibly it's not crashing but also not retaining it long enough to return the result to the caller. – badweasel Nov 07 '13 at 07:30
  • See if it fixes the crash, and still completes the request, without worrying about the leak, I think I know how to fix the leak, but it's not worth going into if it doesn't work in the first place. – Carl Veazey Nov 07 '13 at 08:12
  • Ok yes that retains it long enough to retain the info. So it works now. Since it's working differently in adhoc than in a debug build, how do I even know if it is leaking? (should we move this to chat?) – badweasel Nov 07 '13 at 08:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40709/discussion-between-carl-veazey-and-badweasel) – Carl Veazey Nov 07 '13 at 09:01
  • This is good reference also: http://stackoverflow.com/questions/19227982/using-block-and-weak?rq=1 – badweasel Nov 07 '13 at 09:35
  • http://nshipster.com/pragma/ to hide the hopefully irrelevant warnings after doing your fix. – badweasel Nov 08 '13 at 00:57

0 Answers0