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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:54:14+00:00 2026-05-28T01:54:14+00:00

Consider the following code: @interface ClassA : NSObject @property (nonatomic, copy) void(^blockCopy)(); @end @implementation

  • 0

Consider the following code:

@interface ClassA : NSObject
@property (nonatomic, copy) void(^blockCopy)();
@end

@implementation ClassA

@synthesize blockCopy;

- (void)giveBlock:(void(^)())inBlock {
    blockCopy = inBlock;
}

@end

Then use it in a class which has a strong property of type ClassA called someA:

self.someA = [[ClassA alloc] init];
[self.someA giveBlock:^{
    NSLog(@"self = %@", self);
}];
dispatch_async(dispatch_get_main_queue(), ^{
    self.someA.blockCopy();
    self.someA = nil;
});

If I run that built O3 with ARC enabled, on iOS, it crashes during the self.someA.blockCopy(); call inside objc_retain. Why?

Now I realise that people are probably going to say I should be setting it with self.blockCopy = inBlock but I did kind of think that ARC should be doing the right thing here. If I look at the assembly (ARMv7) produced from the giveBlock: method it looks like this:

        .align  2
        .code   16
        .thumb_func     "-[ClassA giveBlock:]"
"-[ClassA giveBlock:]":
        push    {r7, lr}
        movw    r1, :lower16:(_OBJC_IVAR_$_ClassA.blockCopy-(LPC0_0+4))
        mov     r7, sp
        movt    r1, :upper16:(_OBJC_IVAR_$_ClassA.blockCopy-(LPC0_0+4))
LPC0_0:
        add     r1, pc
        ldr     r1, [r1]
        add     r0, r1
        mov     r1, r2
        blx     _objc_storeStrong
        pop     {r7, pc}

That is calling objc_storeStrong which in turn does a retain on the block and a release on the old block. My guess is that ARC is not properly noticing it’s a block property as I think it should be calling objc_retainBlock instead of the normal objc_retain.

Or, am I just totally wrong and actually ARC is doing what it documents and I’ve just read it the wrong way?

Discussion very much welcome on this – I find this to be rather intriguing.

Points to note:

  • It doesn’t crash on OS X.
  • It doesn’t crash built O0.
  • 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-28T01:54:15+00:00Added an answer on May 28, 2026 at 1:54 am
    - (void)giveBlock:(void(^)())inBlock {
        blockCopy = inBlock;
    }
    

    You need to copy the block either on assignment or when passed into this function. While ARC solves the auto-move-to-heap-on-return problem, it does not do so for arguments (can’t do to idiosyncrasies of C).

    That it doesn’t crash in certain environments is merely coincidental; it won’t crash as long as the stack version of the block hasn’t been overwritten. A sure sign of this is when you have a crash that goes away with optimization turned off. With optimization off, the compiler won’t reuse stack memory within any given scope, causing the memory to be “valid” for long after it should be.


    I still don’t quite understand why it can’t do a objc_blockRetain
    rather than a normal objc_retain, though. The compiler knows the type
    after all.

    I’m pretty sure that the issue is the potential cost of the assignment. If the block captures a lot of state, including, potentially, other blocks, then the Block_copy() might be really really really expensive.

    I.e. if you had something like:

    BlockType b = ^(...) { ... capture lots of gunk ... };
    SomeRandomFunc(b);
    

    … and that implied a Block_copy() merely because of the assignment, it would make it impossible to use blocks consistently without risk of pathological performance issues. Because there is no way for the compiler to know whether SomeRandomFunc() is synchronous or asynchronous, there is no way to manage this automatically (at this time — I’m sure getting rid of this potential tripwire is desirable).

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

Sidebar

Related Questions

Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following code sample: interface IData { int Count(); } interface IOperations {
Consider the following code. public interface IFoo { } public class Bar { public
Consider the following lines of code: public interface IProduct { string Name { get;
Consider the following interface: public interface IMyCallback { void SomeEvent(int someArg); } which is
I would like to understand the ServiceBehavior.ConcurrencyMode property. Consider the following code on service
Consider the following interface. public interface Feed<T> { public void put( T o );
Consider the following code: A.java: import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @interface A{} C.java: import

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.