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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:39:49+00:00 2026-06-13T16:39:49+00:00

Recently I learn that if I need to keep a block object, I should

  • 0

Recently I learn that if I need to keep a block object, I should copy the block, because it’s created on the stack.

Then I review some of my code. I found that I was doing something like:

@implementation MyView
...
-(void) addButton:(NSString *)title clickAction:(void ^(void)) action {
    [self.buttons addObject:@[ title, action ] ];
}
-(void) createButtons { ... }
...
@end

...

// Somewhere in my project:
-(void) init {
     MyView *view = [MyView new];
     [view addButton:@"OK"     clickAction:^{ NSLog(@"OK");     }];
     [view addButton:@"Cancel" clickAction:^{ NSLog(@"Cancel"); }];
}

Basically, I add some buttons info to MyView. At some point, MyView will create the buttons. When the button is click, MyView will find out the corresponding clickAction, and invoke it.

The weird part is that this program works fine, no exception, no error.

So, why the program runs without copying the block object?

Other info :
* iphone app ( 4.3 – 6.0 )
* non-ARC
* XCode 4.5

  • 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-13T16:39:50+00:00Added an answer on June 13, 2026 at 4:39 pm

    Behind the scene, block implementation consists of two parts:

    • The executable code of the block, and
    • A data structure containing the values of the variables used in the block

    Only the second part of the block is placed on the stack and copied to the heap memory; the first part is compiled to the code segment of your program, and does not get copied.

    Since your block implementations do not reference any variable from the surrounding scope, the data portion of their blocks is empty. That is why your code works: there is simply nothing to copy! However, you should add block copy anyway, because otherwise a small change to the code of your block will break your application without the compiler noticing anything.

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

Sidebar

Related Questions

I recently found the need to learn a new library that didn't come with
I have recently started to learn Objective-C and write my tests using OCUnit that
I recently started to learn how to use openCL to speed up some part
I'm recently started to learn Twisted framework and now looking for some cheat sheets/reference
When I started to learn about WPF and MVVM recently, I came across some
Hi I have recently started to learn C# and have some questions regarding properties.
Recently, I feel that I've reached a wall I need to vault over to
Hi I recently started to learn asp.net.I know that when you drag a login
Good day! Recently I have been into backbone/require, and getting to learn some really
I like using Python, because of the easy-to-learn syntax, however, I recently learned it

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.