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

  • Home
  • SEARCH
  • 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 6968651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:28:47+00:00 2026-05-27T16:28:47+00:00

Using Xcode 4.2 and ARC, I wrote the following code before I had any

  • 0

Using Xcode 4.2 and ARC, I wrote the following code before I had any understanding of how blocks needed to be copied from the stack to the heap.

-(void) downloadWithBlock:(void (^)(void))callbackBlock;
{
    // start the data download in the background...
    NSOperation *backgroundOperation = [NSBlockOperation blockOperationWithBlock:^{
        // synchronous download code
    }];
    [backgroundOperationQueue addOperation:backgroundOperation];
    NSOperation *foregroundOperation = [NSBlockOperation blockOperationWithBlock:^{
        callbackBlock();
    }];
    [foregroundOperation addDependency:backgroundOperation];
    [[NSOperationQueue mainQueue] addOperation:foregroundOperation];    
}

The code works, but I don’t trust it because I don’t understand it. In another section of code, I experienced application crashes while invoking blocks that were stored in ivars without using -copy. That has made me wonder whether this section of code should be rewritten like this:

-(void) downloadWithBlock:(void (^)(void))callbackBlock;
{
    void(^heapBlock)(void) = [callbackBlock copy];    
    // start the data download in the background...
    NSOperation *backgroundOperation = [NSBlockOperation blockOperationWithBlock:^{
        // synchronous download code
    }];
    [backgroundOperationQueue addOperation:backgroundOperation];
    NSOperation *foregroundOperation = [NSBlockOperation blockOperationWithBlock:^{
        heapBlock();
    }];
    [foregroundOperation addDependency:backgroundOperation];
    [[NSOperationQueue mainQueue] addOperation:foregroundOperation];    
}

My only concern here is to gain a better understanding of how block pointers work. Is either of these code sections acceptable? Does the invocation of a block within another block trigger the compiler to insert a hidden Block_copy operation?

  • 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-27T16:28:48+00:00Added an answer on May 27, 2026 at 4:28 pm

    Not just the invocation of a block inside a block but any direct reference to one will cause a copy. Including passing it as an argument to something else. Same goes for ObjC types in the block (except it is a simple retain, not a copy for them).

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

Sidebar

Related Questions

Im using Xcode and c++ I have copied the HoughCircles code from the OpenCV
I'm using Xcode for the first time. I've been following a tutorial, and I'm
For my first time using Xcode I was following a tutorial online. I did
I have created an iphone application using the empty template without ARC in xcode
I had created a project using base sdk 5.0 in xcode 4.2. While creating
Using XCode 4.2, I am using the static analyser on code similar to the
Using Xcode 4.x I created a SplitView project for iPad From RootViewController I'm trying
I am using Xcode. I have implemented the shake code. Now I want to
Following the StringCalculator kata from Xcode Katas , I ended up with a following
Using XCode 3.2.3 (64-bit), I get following strange output. What am I doing wrong?

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.