Why does block use copy and how to solve circular references? blockcopy

Source: Internet
Author: User

Why does block use copy and how to solve circular references? blockcopy
During the completion of the project, the block is inevitably used, because the block is more readable than delegate and notification, and the code looks concise. Therefore, a large number of blocks are used in the current project.
I previously introduced the use of proxies and blocks in the development process, and described in detail the similarities between delegate and block. (If you are interested, you can see my previous article link: http://www.cnblogs.com/MasterPeng/p/5210263.html)
It is mainly because, during development, sometimes due to carelessness or some other reasons, the block usage may cause loop reference, resulting in Memory leakage.


The main reason for block loop reference is that external variables are referenced in the block.

The following is a simple example:

Brush. getCardInfo = ^ (NSDictionary * info ){
[Self test];
};


Like the above Code, self is actually a local variable rather than a block internal variable. If it is declared as assign, an error occurs when the code is executed inside the block.

But this brings about another problem, that is, the reference count of self is + 1. This means that loop references may occur. Self holds the brush, the brush holds the block, and the block holds the self. The result is a memory leak.


Solution:
_ Weak CurrentViewController * blockSelf = self;
Brush. getCardInfo = ^ (NSDictionary * info ){
[BlockSelf test];
};

Use _ weak to modify the variable to tell the block variable reference count not to be greater than 1. This avoids the issue of circular references.


Copy is used for block declaration.
Reasons for using copy modification:
The block itself can retain, and release like an object. However, when a block is created, its memory is allocated to the stack instead of heap. The domain itself belongs to the scope at the time of creation. Once block is called outside the scope at the time of creation, the program will crash.
You can also use retain, but the retain action of the block is implemented by the copy action by default, because the block variable is declared as the stack variable by default, in order to be able to be used outside the block declaration, therefore, we need to copy the block to the heap. To ensure that the block attribute declaration is consistent with the actual operation, we recommend that you declare it as copy.


About block more application or implementation, you can refer to a clever god explained block blog link: http://blog.devtang.com/2013/07/28/a-look-inside-blocks/

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.