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