在 Objective-C 中对 Block 应用 property 时的注意事项
?
應當使用:@property (nonatomic, copy)
今天在這個問題上犯錯誤了,找了好久才知道原因。
另外,簡單的進行反匯編看了下,Block 被存儲在靜態變量區,運行時構造出一個運行棧,進行調用。
retain 并不會改變 Block 的引用計數,因此對 Block 應用 retain 相當于 assign。
但是既然在靜態存儲區,為什么會出現 EXC_BAD_ACCESS 呢?代碼都在的呀。
網上都說 Block 在棧上,這應該是錯誤的:指向 Block 代碼的指針在棧上。
我感覺原因是這樣:
執行靜態區的代碼,需要特殊的構造,比如:加載到寄存器,調整好 ESP 等。
而堆上的代碼可以直接執行。
期待更詳細的解釋。
When storing blocks in properties, arrays or other data structures, there’s an important difference between using?copy?or?retain. And in short, you should always use?copy.
When blocks are first created, they are allocated on the stack. If the block is called when that stack frame has disappeared, it can have disastrous consequences, usually a EXC_BAD_ACCESS or something plain weird.
If you?retain?a stack allocated block (as they all start out being), nothing happens. It continues to be stack allocated and will crash your app when called. However, if you?copy?a stack allocated block, it will copy it to the heap, retaining references to local and instance variables used in the block, and calling it will behave as expected. However, if you?copy?a heap allocated block, it doesn’t copy it again, it just?retains?it.
So you should always declare your blocks as properties like this:
@property (copy, ...) (int)(^aBlock)();And never like this:
@property (retain, ...) (int)(^aBlock)();And when providing blocks to?NSMutableArrays and the like, always?copy, never?retain.
轉載于:https://www.cnblogs.com/Proteas/archive/2012/06/26/2563747.html
總結
以上是生活随笔為你收集整理的在 Objective-C 中对 Block 应用 property 时的注意事项的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js触发asp.net的Button的O
- 下一篇: 【英语天天读】Man's Youth