Why the block of iOS is decorated with copy

Source: Internet
Author: User

Before you read through the article, you may need to understand the basics of memory allocation.


By default, the Blockis archived on the stack and may be recycled at any time, and can be kept in a heap by copy, which is equivalent to being strongly referenced, so if you use self in the block, you need to weaken it by __weak or __unsafe_  unretained. Here is the sample code and its description, and the reader can try to print out the block memory in different situations


when the function's internal code ends, all variables stored in the stack in the function are released by the system, so if the property's block is assign decorated, wild pointer access occurs when it is accessed again.


[OBJC]  View plain  copy #import   "ViewController.h"       @interface  viewcontroller   ()    @property   (nonatomic, copy)  void (^myblock) ();      @end       @implementation  ViewController     -  (void) viewdidload  {       [super viewDidLoad];              //1 __NSGlobalBlock__   global block    stored in code area (storage method or function)         void (^MYBLOCK1) ()  = ^ ()  {            nslog (@ "I'm the Boss");       };       nslog (@ "%@", MyBlock1);                      //2 __NSStackBlock__   Stack block   stored in stack area      &NBSp; //block internal Access external variables        //block's essence is a structural body         int n = 5;       void (^myblock2) ()  = ^ ()  {            nslog (@ "I'm dick%d",  n);        };       nslog (@ "%@",  myblock2);                       //3 __NSMallocBlock__   Heap block  stored in heap area    do a copy operation on stack block        void (^MYBLOCK3) ()  =  ^ ()  {           nslog (@ "I'm dick%d",  n);        };       nslog (@ "%@",  [myblock3 copy]);                              /*        The above three examples show that blocks are stored in the code area when they do not have access to external variables,         when block access to external variables is stored in the stack area,  and at this point the scope will be released          The following example:       */       [self test];// When this code ends, all variables stored in the stack area in the test function are released by the system,  so if the property's block is assign decorated    a wild pointer access occurs when you access .        self.myblock ();        }     -  (void) test {        int n = 5;       [self setMyblock:^{            nslog (@ "%d", N);       }]; &nbs

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.