Considerations when applying the property to a Block in Objective-C

Source: Internet
Author: User

 

Use: @ property (nonatomic, copy)

I made a mistake on this issue today. I have been searching for a long time to find out the cause.


In addition, after a simple disassembly, the Block is stored in the static variable area, and a runtime stack is constructed during runtime for calling.

Retain does not change the reference count of the Block. Therefore, applying retain to the Block is equivalent to assign.

But why does EXC_BAD_ACCESS appear in the static storage area? The code is all there.

It is said on the Internet that the Block is on the stack, which should be wrong: the pointer pointing to the Block code is on the stack.

I think the reason is as follows:

Code execution in the static zone requires special structures, such as loading to registers and adjusting ESP.

The Code on the stack can be directly executed.

We look forward to more detailed explanations.



When storing blocks in properties, arrays or other data structures, there's an important difference between usingcopyOrretain. And in short, you shoshould always usecopy.

When blocks are first created, they are allocated on the stack. If the block is called when that stack frame has disappeared, it can have disastrous consequences, usually a EXC_BAD_ACCESS or something plain weird.

If youretainA stack allocated block (as they all start out being), nothing happens. It continues to be stack allocated and will crash your app when called. However, if youcopyA stack allocated block, it will copy it to the heap, retaining references to local and instance variables used in the block, and calling it will behave as expected. However, if youcopyA heap allocated block, it doesn' t copy it again, it justretainsIt.

So you shoshould always declare your blocks as properties like this:

@property (copy, ...) (int)(^aBlock)();

And never like this:

@property (retain, ...) (int)(^aBlock)();

And when providing blocksNSMutableArrayS and the like, alwayscopy, Neverretain.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.