Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:30:50+00:00 2026-06-08T22:30:50+00:00

I have been using blocks for some time now, but I feel that there

  • 0

I have been using blocks for some time now, but I feel that there are things I miss about memory management in both ARC and non-ARC environments. I feel that deeper understanding will make me void many memory leaks.

AFNetworking is my main use of Blocks in a particular application. Most of the time, inside a completion handler of an operation, I do something like “[self.myArray addObject]”.

In both ARC and non-ARC enabled environments, “self” will be retained according to this article from Apple.

That means that whenever a completion block of an AFNetworking network operation is called, self is retained inside that block, and released when that block goes out of scope. I believe that this applies to both ARC and non-ARC. I have ran both the Leaks tool and the Static Analyzer so that I may find any memory leaks. None showed any.

However, it wasn’t until recently that I stumbled upon a warning which I couldn’t figure out. I am using ARC in this particular example.

I have two instance variables that indicate the completion and failure of a network operation

@property (nonatomic, readwrite, copy) SFCompletionBlock completionBlock;
@property (nonatomic, readwrite, copy) SFFailureBlock failureBlock;
@synthesize failureBlock = _failureBlock;
@synthesize operation = _operation;

Somewhere in the code, I do this:

[self.operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id
                                                    responseObject) {
NSError *error = [NSError errorWithDomain:@"com.test" code:100 userInfo:@{@"description": @"zero results"}];
            _failureBlock(error);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"nothing");
        }];

Xcode complains about the line that calls the failureBlock, with the message “Capturing “self” strongly in this block is likely to result in a retain cycle. I believe Xcode is right: the failure block retains self, and self holds its own copy of the block, so none of the two will be deallocated.

However, I have the following questions/observations.

1) If i change _failureBlock(error) to “self.failureBlock(error)” (without quotes) the compiler stops complaining. Why is that? Is this a memory leak the compiler misses?

2) In general, what is the best practice to work with blocks in both ARC and non-ARC enabled environments when using blocks that are instance variables? Seems that in the case of completion and failure blocks in AFNetworking, those two blocks are not instance variables so they probably don’t fall into the category of retain cycles that I described above. But when using progress blocks into AFNetworking, what can be done to avoid retain cycles like the one above?

I would love to hear other people’s thoughts on ARC and non-ARC with blocks and issues/solutions with memory management. I find these situations error-prone and I feel some discussion on this is necessary to clear things up.

I don’t know if it matters, but I use Xcode 4.4 with the latest LLVM.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:30:52+00:00Added an answer on June 8, 2026 at 10:30 pm

    1) If i change _failureBlock(error) to “self.failureBlock(error)”
    (without quotes) the compiler stops complaining. Why is that? Is this
    a memory leak the compiler misses?

    The retain cycle exists in both cases. If you’re targeting iOS 5+, you can pass in a weak reference to self:

    __weak MyClass *weakSelf;
    [self.operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error = [NSError errorWithDomain:@"com.test" code:100 userInfo:@{@"description": @"zero results"}];
        if (weakSelf.failureBlock) weakSelf.failureBlock(error);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"nothing");
    }];
    

    Now self won’t be retained, and if it’s deallocated before the callback is invoked, the callback is a no-op. However, it’s possible that it could be undergoing deallocation while the callback is invoked on a background thread, so this pattern can create occasional crashes.

    2) In general, what is the best practice to work with blocks in both
    ARC and non-ARC enabled environments when using blocks that are
    instance variables? Seems that in the case of completion and failure
    blocks in AFNetworking, those two blocks are not instance variables so
    they probably don’t fall into the category of retain cycles that I
    described above. But when using progress blocks into AFNetworking,
    what can be done to avoid retain cycles like the one above?

    Most of the time, I think it’s better not to store blocks in instance variables. If you instead return the block from a method in your class, you’ll still have a retain cycle, but it only exists from the time the method is invoked to the time the block is released. So it will prevent your instance from being deallocated during the block execution, but the retain cycle ends when the block is released:

    -(SFCompletionBlock)completionBlock {
        return ^(AFHTTPRequestOperation *operation , id responseObject ) {
            [self doSomethingWithOperation:operation];
        };
    }
    
    [self.operation setCompletionBlockWithSuccess:[self completionBlock]
                                          failure:[self failureBlock]
    ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using MVVM (or my own flavour thereof) for some time now, but
I've been using Jquery for some time now and I have a question concerning
I have been using TortoiseSVN for some time and I really like it. I
Have been working on a Tower Defense game for iOS for some time now.
I've been a web developer for really quite some time now, but this is
I have been using an API to do some work. This is how I
I have been using some Javascript to create a text field once an certain
I have been using the storyboard to make an application and currently there are
I've been using http://code.google.com/p/plblocks/ for a while now to get blocks support in our
I have been messing with this code for quite some time, works in FF,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.