IOS block types and switching

Source: Internet
Author: User

Block is divided into three kinds of nsglobalblock,nsstackblock, Nsmallocblock.

    • Nsglobalblock: Similar function, in text segment;
    • Nsstackblock: In stack memory, block will be invalid after function return;
    • Nsmallocblock: In heap memory.

Nsglobalblock we can identify by referencing an external variable, which is nsglobalblock if the external variable is not referenced, and can be used as a function. Otherwise it is nsstackblock.

Nsmallocblock only need to copy the Nsstackblock to get it, but retain operation is not possible

    • Block_copy is equivalent to copy, and Block_release is equivalent to release.
    • The reference count is not changed for block retain, copy, Release Retaincount,retaincount is always 1;
    • Nsglobalblock:retain, copy, release operations are not valid;
    • Nsstackblock:retain, release operation is not valid, it must be noted that nsstackblock after the function is returned, block memory will be recycled. Even retain is useless. The easy mistake is [[Mutableaarry Addobject:stackblock], (complement: Do not worry about this in arc, because the instantiated block is copied to the heap by default in arc) after the function is out of the stack, The stackblock that were taken from the Mutableaarry have been recycled and turned into wild pointers. The correct approach is to copy the Stackblock to the heap first, then add the array: [Mutableaarry addobject:[[stackblock copy] autorelease]. A new Nsmallocblock type object is generated after Copy,copy is supported.
    • Nsmallocblock supports retain, release, although Retaincount is always 1, the memory manager will still increase and decrease the count. No new objects are generated after copy, only one reference is added, similar to retain;
    • Try not to use the retain operation on the block.

For more detailed block content, please visit the original blog: http://www.cnblogs.com/hanjun/p/3767394.html

IOS block types and switching

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.