? ??? 對(duì)Block的內(nèi)存使用相關(guān)的內(nèi)容簡要整理,解釋其中的道理和使用Block需要注意的問題匀奏。
1. Block與對(duì)象
首先我們先反思幾個(gè)問題:
block到底是不是對(duì)象?
如果是對(duì)象娃善,和某個(gè)已定義的類的實(shí)例對(duì)象在使用上是不是一樣的瑞佩?
如果不一樣,主要的區(qū)別是什么炬丸?
對(duì)于第一個(gè)問題,蘋果的Objective-C官方文檔中在“Working with Blocks”明確說明:
“Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary.”
可見,Block是Objective-C語言中的對(duì)象焕阿。
蘋果在block的文檔中也提過這么一句:
“As an optimization, block storage starts out on the stack—just like blocks themselves do.”
Clang的文檔中也有說明:
“The initial allocation is done on the stack,but the runtime provides aBlock_copyfunction” (Block_copy在下面我會(huì)說)
憑這一點(diǎn)暮屡,我們就可以回答剩下的兩個(gè)問題。Block對(duì)象與一般的類實(shí)例對(duì)象有所不同栽惶,一個(gè)主要的區(qū)別就是分配的位置不同,block默認(rèn)在棧上分配外厂,一般類的實(shí)例對(duì)象在堆上分配冕象。而這正是導(dǎo)致本文最初提到的那個(gè)問題發(fā)生的根本原因汁蝶。Block對(duì)象在棧上分配时肿,block的引用指向棧幀內(nèi)存,而當(dāng)方法調(diào)用過后猜煮,指針指向的內(nèi)存上寫的是什么數(shù)據(jù)就不確定了琳疏。但是到此,retain的疑問還是沒有解開帕棉。
我們想一想Objective-C引用計(jì)數(shù)的原理,retain是對(duì)一個(gè)在堆中分配內(nèi)存的對(duì)象的引用計(jì)數(shù)做了增加香伴,執(zhí)行release操作的時(shí)候檢查計(jì)數(shù)是否為1,如果是則釋放堆中內(nèi)存即纲。而對(duì)于在棧上分配的block對(duì)象,這一點(diǎn)顯然有所不同低斋,如果方法調(diào)用返回蜂厅,棧幀上的數(shù)據(jù)自然會(huì)作廢處理拔稳,不像堆上內(nèi)存,需要單獨(dú)release巴比,就算NSArray對(duì)block對(duì)象本身做了retain也無濟(jì)于事礁遵。
Clang文檔中提到:
“Block pointers may be converted to typeid; block objects are laid out in a way that makes them compatible with Objective-C objects. There is a builtin class that all block objects are considered to be objects of; this class implementsretainby adjusting the reference count, not by callingBlock_copy.”
那么要是用一個(gè)方法對(duì)block數(shù)組做初始化是否有可行方案呢。答案是肯定的佣耐,不過需要真正了解block的使用,至少要會(huì)用Block_copy()和Block_release()兼砖。
2. Block的類型和使用
在Clang的文檔中,定義了兩個(gè)Block類型:?_NSConcreteGlobalBlock
和?_NSConcreteStackBlock既棺。
因此,也就得到了下面對(duì)block的使用注意點(diǎn)丸冕。
對(duì)于Global的Block,我們無需多處理胖烛,不需retain和copy,因?yàn)榧词鼓氵@樣做了佩番,似乎也不會(huì)有什么兩樣众旗。對(duì)于Stack的Block趟畏,如果不做任何操作,就會(huì)向上面所說赋秀,隨棧幀自生自滅。而如果想讓它獲得比stackframe更久沃琅,那就調(diào)用Block_copy()蜘欲,讓它搬家到堆內(nèi)存上。而對(duì)于已經(jīng)在堆上的block姥份,也不要指望通過copy進(jìn)行“真正的copy”郭脂,因?yàn)槠湟玫降淖兞咳匀粫?huì)是同一份,在這個(gè)意義上看澈歉,這里的copy和retain的作用已經(jīng)非常類似。
“The runtime provides aBlock_copyfunction which, given a block pointer, either copies the underlying block object to the heap, setting its reference count to 1 and returning the new block pointer, or (if the block object is already on the heap) increases its reference count by 1. The paired function isBlock_release, which decreases the reference count by 1 and destroys the object if the count reaches zero and is on the heap.”
在類中埃难,如果有block對(duì)象作為property涤久,可以聲明為copy。
3. 其它
???? 在蘋果官方的《Transitioning to ARC Release Notes》文檔中响迂,寫了這樣一段話,大家理解一下细疚,尤其是其中的“just work”。
“How do blocks work in ARC?
Blocks ‘just work’ when you pass blocks up the stack in ARC mode,
such as in a return. You don’t have to call Block Copy any more.”
4. 參考
以上整理了對(duì)Block的理解疯兼,在開發(fā)中注意到這些點(diǎn)足以解決block的特殊性帶來的各類問題。要想繼續(xù)深入吧彪,可參看LLVM文檔中對(duì)block的介紹:
http://clang.llvm.org/docs/Block-ABI-Apple.html
http://clang.llvm.org/docs/AutomaticReferenceCounting.html?highlight=class