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 7021811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:29:57+00:00 2026-05-27T23:29:57+00:00

It’s a common pattern in my code to allocate an object, let it do

  • 0

It’s a common pattern in my code to allocate an object, let it do some stuff with a completion handler and release it in the handler:

LongOperation *foo = [[LongOperation alloc] init];
[foo runWithCompletion:^{
    // run some code and then:
    [foo autorelease];
}];

This works fairly well, but when I attempt to convert the code to ARC, Xcode rightly complains that it can’t simply drop the autorelease from the block, since that would make the foo object get deallocated after leaving the scope.

So what’s a good way to write this kind of pattern under ARC? I could introduce an instance variable for foo:

[self setFoo:[[LongOperation alloc] init]];
[foo runWithCompletion:^{
    // run some code and then:
    [self setFoo:nil];
}];

…but the code would not be re-entrant anymore.

  • 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-05-27T23:29:58+00:00Added an answer on May 27, 2026 at 11:29 pm

    In most cases, it should work (i.e., if anything references self inside of foo, foo will last long enough to satisfy that code before going away). If there are issues with weak references and such that foo looks like it should go away but shouldn’t until after the handler runs, you can do something like:

    __block LongOperation* foo = [[LongOperation alloc] init];
    [foo runWithCompletion:^{
      // do some things
      foo = nil;
    }];
    

    Note this is kind of the opposite of this pattern which causes the object /not/ to be captured under managed memory rules.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've noticed a common pattern in some code I'm writing so I decided to
This is becoming a common pattern in my code, for when I need to
I'm running into a common pattern in the code that I'm writing, where I
What are some common , real world examples of using the Builder Pattern? What
I am trying to extract out the common code pattern here in an extract
I find myself repeating a common pattern when trying to execute code across multiple
There are some common code patterns I find in other people's Java code that
A common pattern in C++ is to create a class that wraps a lock
Is it a common pattern/idiom to use free functions as pseudo-constructors to avoid having
I'm working with a fairly common pattern - a page opens a child window

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.