Objective-C Blocks Under the Hood
来源:互联网 发布:淘宝刷两单会坐牢吗 编辑:程序博客网 时间:2024/06/04 18:14
Recently I was asked to describe the ‘under the hood’ memory management of variable and objects in blocks. I think that in 2013 almost every iOS dev knows the practical usage and effect of putting the __block
keyword before the declaration of a variable, but maybe what’s happening for real is not so straightforward.
Heads up:
- Blocks are created on the stack
- Blocks can be copied to the heap
- Blocks have their own private const copies of stack variables (and pointers)
- Mutable stack variables and pointers must be declared with the __block keyword
If blocks aren’t kept around anywhere, will remain on the stack and will go away when their stack frame returns. While on the stack, a block has no effect on the storage or lifetime of anything it accesses. If blocks need to exist after the stack frame returns, they can be copied to the heap and this action is an explicit operation. This way, a block will gain reference-counting as all objects in Cocoa. When they are copied, they take their captured scope with them, retaining any objects they refer. If a block references a stack variable or pointer, then when the block is initialized it is given its own copy of that variable declared const, so assignments won’t work. When a block is copied, the __block
stack variables it reference are copied to the heap and after the copy operation both block on the stack and brand new block on the heap refer to the variables on the heap.
Apple documentation helped me in some ways to make things clear, also this article, but to clearly see what’s going on in memory I arrange a little exercise:
LLDB shows that a block is a nice piece of things.
The most important thing to note is that __block
variables and pointers are treated inside the block as structs that obviously handle the reference to the real value/object.
Ready? Here is the log. With explanation!
Blocks are first-class citizens in the Objective-C runtime: they have an ‘isa’ pointer which defines a class through which the Objective-C runtime can access methods and storage. In a non-ARC environment you can mess things up for sure, and cause crashes due to dangling pointers. __block
applies only when using the variables in the block, it simply says to the block:
Hey, this pointer or primitive type relies on the stack with its own address. Please refer to this little friend with a new variable on the heap. I mean… refer to the object with double dereference, and don’t retain it.
Thank you, sir.
If at some time after the declaration but before the invocation of the block, the object has been released and deallocated, the execution of the block will cause a crash. __block
variables are not retained within the block. In the deep end… it’s all about pointers, references, dereferences and retain count stuff. As expected.
- Objective-C Blocks Under the Hood
- C++: Under the Hood
- C++: Under the Hood
- C++: Under the Hood
- C++/CLI keywords: Under the hood
- FlushViewOfFile Under the Hood
- Android - under the hood
- Redis: under the hood
- Redis: under the hood
- Redis: under the hood
- MFC under the hood
- under the hood
- Under the Hood
- Redis: under the hood
- Under The Hood
- Redis: under the hood
- Redis : under the hood
- C++ Under the Hood
- Round 1 D
- CBUS转MQTT
- Material Design笔记
- 【洛谷2744 】【CJOJ1804】[USACO5.3]量取牛奶Milk Measuring
- application.properties 改成 application.yml
- Objective-C Blocks Under the Hood
- linux 常用操作符号
- python 单行注释 多行注释 批量注释 中文注释
- dwz选择带回功能dialog不能翻页解决方法
- python -- 函数总结
- 20、Warning: there were * unresolved references to classes or interfaces.
- Spring源码分析2 — 容器启动流程
- 添加到父类视图后,subview的高度莫名发生变化
- JEESZ RestFul服务介绍