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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:49:23+00:00 2026-06-18T04:49:23+00:00

I’m transitioning to ARC (Automatic Reference Counting) and this document was very helpful: Transitioning

  • 0

I’m transitioning to ARC (Automatic Reference Counting) and this document was very helpful: Transitioning to ARC Release Notes. It says:

How do blocks work in ARC?

Blocks “just work” when you pass blocks up the stack in ARC mode, such as in a return. You don’t have to call Block Copy any more. You still need to use [^{} copy] when passing “down” the stack into arrayWithObjects: and other methods that do a retain.

Can somebody elaborate on this a little bit or give some example? I understood that to mean usually I don’t need to do [block copy] anymore even if the block is used outside the declared scope as ARC now will take care of it more smartly, but I still need to copy it when I want to pass the block to functions that would retain it. Is this correct?

Actually I’ve experimented with it a little bit and so far it seems okay even if I pass blocks to arrays without copying, so the question. Thanks!

  • 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-18T04:49:24+00:00Added an answer on June 18, 2026 at 4:49 am

    Let’s consider the problem from an MRC (manual reference counting) perspective. When you are returning a stack block from a function, it is always the right thing to do to return a copy of it (usually with [[ copy] autorelease]). This is because the lifetime of the stack block is that function call, which is ending. So in order for the returned pointer to point to a valid block after returning, of course you have to return a heap copy.

    However, what about for passing a block as an argument to a function or method. Should you pass a copy of it? It’s a little more complicated. We know that, if you need to store a block for later use in a place that will outlive the function call (e.g. in an instance variable or global variable, etc.), you need to store a copy of the block. But how do we know when this is true (and if it is true, which function (caller/callee) is responsible for copying it)?

    Generally, when you pass it to a function or method whose parameter is of block type, then you don’t need to copy it, because, as a function that takes a block parameter, that function is responsible for copying it if it needs to store it around for later.

    But what about passing a block to a function or method whose parameter is of non-block type (like id)? Then that function does not know that its argument is a block, and so will only retain the object if it needs to store it for later, not copy it. In this case, if we know that the function or method will store the object around for later (e.g. with -[NSMutableArray addObject:]), we should pass a copy of the block. Should we always pass a copy? Not necessarily. If we know the function or method will not store the block for later (e.g. to just print the object), it is inefficient to make a copy of the block.

    In ARC, the compiler tries to do as much as possible for you. So in the case of returning a stack block (“passing it up”), the compiler knows that returning a copy is always the right thing, so it implicitly does that. That’s why you don’t need to do it yourself anymore.

    However, for passing a block to a method or function (“passing it down”), it is not always possible to automatically determine whether copying is needed or not, and extra copying might be inefficient, so the compiler doesn’t do anything automatically; and you the programmer needs to figure it out.

    I’ve noticed that in recent versions of the compiler, ARC copies blocks in a lot more situations that I thought, so perhaps they changed the implementation of ARC to pretty much always copy blocks, in order to err on the side of caution, and they believed that the performance cost is not as important. So it is possible that it will work now passing a block directly to addObject: without explicitly copying. But I believe that this was not always the case with ARC, and there is no guarantee in the language for this, or that this behavior will stay in the future.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.