1

I'm having trouble figuring out why a __weak reference to self is disappearing in the following block. Following Jiva's advice found here http://blog.random-ideas.net/?p=160 I make a __weak reference to self outside the completion block, then make a strong reference to that weak reference within the block to hold on to it for the duration of the block.

The problem I'm having is that the weak reference (weakSelf) comes up nil when the block executes, but if I just make the call to self directly in the block everything works.

- (void)fetchItemList:(NSArray *)itemList withHandler:(ItemFetchCompletion)completionHandler
{
    NSString *fetchString = [self fetchRequestUsingItemList:itemList];
    NSURL *fetchUrl = [NSURL URLWithString:fetchString];
    ASIHTTPRequest *itemListRequest = [ASIHTTPRequest requestWithURL:fetchUrl];
    __weak FetchManager *weakSelf = self;   
    __weak ASIHTTPRequest *itemListRequestWeakRef = itemListRequest;
    [itemListRequest setCompletionBlock:^{
        ASIHTTPRequest *itemListRequestStrongRef = itemListRequestWeakRef;
        ZAssert(weakSelf, @"weakSelf reference is nil.");
        FetchManager *strongSelf = weakSelf;        
        NSData *fetchedData = [itemListRequestStrongRef responseData];
        NSArray *fetchedItems = [strongSelf itemsFromFetchedData:fetchedData];
        completionHandler(fetchedItems);
    }];
    [itemListRequest startAsynchronous];
}

1 Answer 1

4

Here is essentially what your code is doing:

  1. Making a weak variable, weakSelf.
  2. Passing a block to a function that references that weak variable.

A this point, self is not retained by the block nor the weakSelf variable. Then, when the block runs, weakSelf is put into a __strong variable, thus retaining any value that was in weakSelf. Unfortunately, if the original value of weakSelf, namely self was already deallocated, weakSelf would be nil already.

The compiler can see what external variables your block accesses, and then adds any necessary retains if those variables are __strong. The compiler does not care that you assign a __weak variable to an internal variable that happens to be __strong. This retain will happen when the block runs, not when it is created.

By using self directly from the block, the compiler sees that you are referencing a variable that is not __weak, and therefore automatically retains that variable for the lifecycle of the block. In cases like this, I see no reason why you wouldn't directly reference self in the block, unless you are worried about a circular retain cycle.

2
  • Right, the weak/strong ref dance was to avoid a retain cycle. I don't know what was going on but there's nothing wrong with the code and, oddly enough, it works perfectly now. Thanks!
    – user1014780
    Oct 27, 2011 at 18:31
  • 1
    Circular references shouldn't be a problem unless self keeps a strong reference to the block, which doesn't seem to be the case.
    – Rad'Val
    Jan 27, 2013 at 4:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.