Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using blocks within blocks in Objective-C: EXC_BAD_ACCESS

Using iOS 5's new TWRequest API, I've ran into a brick wall related with block usage.

What I need to do is upon receiving a successful response to a first request, immediately fire another one. On the completion block of the second request, I then notify success or failure of the multi-step operation.

Here's roughly what I'm doing:

- (void)doRequests
{
    TWRequest* firstRequest = [self createFirstRequest];
    [firstRequest performRequestWithHandler:^(NSData* responseData,
                                              NSHTTPURLResponse* response,
                                              NSError* error) {
        // Error handling hidden for the sake of brevity...
        TWRequest* secondRequest = [self createSecondRequest];
        [secondRequest performRequestWithHandler:^(NSData* a,
                                                   NSHTTPURLResponse* b,
                                                   NSError* c) {
            // Notify of success or failure - never reaches this far
        }];
    }];
}

I am not retaining either of the requests or keeping a reference to them anywhere; it's just fire-and-forget.

However, when I run the app, it crashes with EXC_BAD_ACCESS on:

[secondRequest performRequestWithHandler:...];

It executes the first request just fine, but when I try to launch a second one with a handler, it crashes. What's wrong with that code?


The methods to create the requests are as simple as:

- (TWRequest*)createFirstRequest
{
    NSString* target   = @"https://api.twitter.com/1/statuses/home_timeline.json";
    NSURL* url         = [NSURL URLWithString:target];
    TWRequest* request = [[TWRequest alloc]
                          initWithURL:url parameters:params
                          requestMethod:TWRequestMethodGET];
    // _twitterAccount is the backing ivar for property 'twitterAccount',
    // a strong & nonatomic property of type ACAccount*
    request.account    = _twitterAccount;

    return request;
}
like image 373
biasedbit Avatar asked Dec 28 '11 22:12

biasedbit


2 Answers

Make sure you're keeping a reference/retaining the ACAccountStore that owns the ACAccount you are using to sign the TWRequests.

If you don't, the ACAccount will become invalid and then you'll get EXC_BAD_ACCESS when trying to fire a TWRequest signed with it.

like image 159
Fred Oliveira Avatar answered Sep 20 '22 02:09

Fred Oliveira


I'm not familiar with TW*, so consider this a wild guess ... try sending a heap-allocated block:

[firstRequest performRequestWithHandler:[^ (NSData *responseData, ...) {
  ...
} copy]];

To clarify, I think the block you're sending is heap-allocated, so while TW* might be retaining it, it won't make any difference if it has already gone out of scope.

like image 43
senojsitruc Avatar answered Sep 20 '22 02:09

senojsitruc