Deep dive into blocks----------memory management

Source: Internet
Author: User
The essence of block is implemented by C language structure in the bottom block, and the essence of block in OC is OC object.
The OC code is re-CLANG-REWRITE-OBJC through "

^{printf ("Tempblock"); This block will be converted into the following structure:
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *funcptr;
};

static struct __main_block_desc_0 {
SIZE_T reserved;
size_t Block_size;
} __main_block_desc_0_data = {0, sizeof (struct __main_block_impl_0)};

struct __MAIN_BLOCK_IMPL_0 {
struct __block_impl impl;
struct __main_block_desc_0* desc;
__main_block_impl_0 (void *fp, struct __main_block_desc_0 *desc, int flags=0) {
Impl.isa = &_NSConcreteStackBlock;
Impl. Flags = flags;
Impl. funcptr = FP;
desc = desc;
}
};

Each OC object has an Isa pointer to its class object, similar to what you can see in the converted code "Impl.isa = &_NSConcreteStackBlock;" The Isa pointer of the block points to the "_nsconcretestackblock" class object. That is, the block is a _nsconcretestackblock class object.

In addition to _nsconcretestackblock, there are two similar classes of _nsconcreteglobalblock and _nsconcretemallocblock. Different block class objects indicate that different storage domains correspond to different OC types, as follows:

Clang class OC class Storage domain
_nsconcreteglobalblock __nsglobalblock__ static data area
_nsconcretestackblock __nsstackblock__ Stack
_nsconcretemallocblock __nsmallocblock__ Heap
Only two types of _nsconcretestackblock and _nsconcreteglobalblock are defined by CLANG-REWRITE-OBJC block. There are three types of blocks in OC for the sake of understanding we have added "_nsconcretemallocblock", the block type in OC can be viewed by simple "NSLog".

Second, _nsconcreteglobalblock usually block is the _nsconcretestackblock class object, are set on the stack. But that's not all:
Automatic variables cannot be used where global variables are used, so there is no interception of automatic variables. The contents of this block's struct instance do not depend on the state of execution, so the entire program needs only one instance. Therefore, the block is set with the struct instance in the same data region as the global variable.

Even if the block syntax is used within a function, the contents of the block's struct instance are also not dependent on the execution state when the block does not intercept any variables, so the entire program also requires only one instance, at which point the block struct instance is also set to the static data area. In this case, the source code that is converted through CLANG-REWRITE-OBJC is usually the _nsconcretestackblock class object, but in fact it is different, and the object obtained by NSLog is "__nsglobalblock__". This is summarized as follows: The block generated when using the block syntax where the global variable is recorded is the _nsconcreteglobalblock class object. Block is the _nsconcreteglobalblock class object that is generated when you do not use variables that should be intercepted in an expression in block syntax.
Third, the _nsconcretemallocblock is configured on the global variable block, from the scope of the variable can also be used by the pointer security. But the block that is set on the stack, if it belongs to the scope of the variable to end. The block is discarded, and accessing the block from outside the scope of the variable causes the program to crash. Blocks provides a way to copy blocks from the stack onto the heap to solve this problem. The block configured on the stack is copied to the heap so that the block on the heap can continue even if the scope of the variable scoped by the block syntax is over.     When a block on a stack is copied to the heap, its memory management is the same as an OC object (reference count management). Simply copy the block from the stack to the heap in a non-arc environment, and be aware that the block after copy is released to prevent memory leaks.
In non-ARC environments, when a method returns a block, the usual practice is: return [[Stackblock copy] autorelease]; The copy operation copies the block to the heap and autorelease the operation to prevent a memory leak.

In the ARC environment, most of the time the compiler makes the right decision to automatically generate code that replicates the block from the stack to the heap, as summarized below:
When block is referenced by the strong type pointer when block is returned as a method return value when the Block property is decorated with the copy modifier
The copy operation for different types of blocks is summarized as follows:
Class of Block Storage domain Copy effect
_nsconcreteglobalblock static data area I don't do anything.
_nsconcretestackblock Stack Copy from stack to heap
_nsconcretemallocblock Heap Increase reference count

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.