Two points of attention for iOS using block--3 block

Source: Internet
Author: User

First of all, to popularize memory storage area knowledge:

Memory is divided into five areas: heap, stack, global zone, literal constant area, code areaHeap Storage: A block of memory allocated by new, ALLOC, controlled by the programmer to release P1 = (char *) malloc;Stack storage: Store function parameters, local variables, variable stores that are automatically purged by the compiler when they are not needed.   int b;   Char s[] = "ABC"; CHAR*P2; Global (Static): Global variables and static variables stored in a block, after the end of the program by the system to release the text constant area: the constant string is placed here, the program ends the system frees the Code area: the binary code that holds the function body 1 Block memory potential under MRCQuestion: 1-Is there a problem with this code in arc?     There is no 2-is there a problem with this code in MRC? exists, after setting block, callback block at the appropriate time, do not want block to be released, so we need to copy the block
typedefvoid(^Blocktype) ();//a function that defines the return value as a block typeBlocktype Test () {inti =5; Blocktype BB= ^{NSLog (@ "xxxx i= %d ", i); }     //return BB; //prevent being released, copy to the heap area, so need to change to return Block_copy (BB);     returnblock_copy (BB);}intMain () {@autoreleasepool {//receive the return value of testBlocktype B1 =test (); //Execute blockB1 (); //MRC next copy to the heap area, need to release the Block control, so you need to add a sentence Block_release (B1 ());Block_release (B1 ()); }}

   2 Block Circular referenceProblem: A strong reference to the block after the block has been copy,copy in the controller, the self of the block strongly references the controller, resulting in a strong reference ring that cannot be released from one another Solve the problem: to break the strong citation ring as long as the self into a weak pointer in the block, you can solve;
// the magic of this method is: typeof (self) weakself = self;   Change the self in block to Weakself

Finally I read in the process of the post pasted: —————— memory ———————— Stack differences: http://blog.csdn.net/aixiaolin/article/details/6863991:// blog.csdn.net/wealoong/article/details/8686353 —————— block ———————— arc and non-ARC block properties problem: http://www.mgenware.com/ blog/?p=1493 three scenarios prone to "circular referencing": Http://www.2cto.com/kf/201409/332193.htmlblock Use test: http://www.cocoachina.com/bbs/ Read.php?tid=152222&page=1

Two points of attention for iOS using block--3 block

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.