Pro Multithreading and Memory Management for iOS and OS X: with ARC,Grand Central Dispatch,and Blocks ? 讀書。
Life Before Automatic Reference Counting
MRC---------《1》array沒有擁有權(quán)儒飒,必須retain之后才有富稻,才有擁有權(quán)蜀变,才能去釋放。
? ? ? ? ? ? ? ?---《2》allocObject 如果以alloc開頭就是這個方法有了擁有權(quán)挺举。調(diào)用改方法汹粤,對象也具有擁有權(quán)箩做。
------《3》[NSMutableArray ? array] ?在方法里面autorelese 用釋放池自動釋放虫腋。所以調(diào)用它時候沒有擁有權(quán)更不能release. ? ??
----《4》沒有擁有權(quán)不可以release,否則會崩掉骄酗。
有擁有權(quán)就可以release,如果多release 會及時或者稍候崩掉,crash悦冀。
GNUstep implementation.---------------------Implementing alloc, retain, release, and dealloc ?
GNUstep《1》查看gnustep的nsobject的源碼發(fā)現(xiàn)趋翻。alloc的創(chuàng)建。
創(chuàng)建 ~1個size大小的空間盒蟆。size包括結(jié)構(gòu)體的大小+實列化大小?
用0填充 函數(shù)返回一個指向分配起始地址的指針分配的size是以struct_layout起始的,向后移動一位的指針返回,id類型踏烙。The alloc method returns a memory block filled with zero containing a struct obj_layoutheader, which has a variable “retained” to store the number of references. This number
is called the reference count. Figure 1–8 shows the structure of an object in the
---------------- >-----(alloc方法返回用0填充的內(nèi)存塊,這個內(nèi)存塊的頭是一個obj_layout結(jié)構(gòu)體历等,結(jié)構(gòu)體)里面包含一個變量? ‘retained’來存儲引用計數(shù)的讨惩。調(diào)用obj.retainCount]就是調(diào)用這個值。
alloc 被調(diào)用之后引用系數(shù)+1.
GNUstep <2>retain method? 返回結(jié)構(gòu)體中的變量 retained? retaiCount 結(jié)構(gòu)體中retained值+1.
?GNUstep <3>release method ? ? ?release使結(jié)構(gòu)體中的變量值減少募闲,當(dāng)變量值為0的時候就會dealloc掉(self)該對象 步脓,free(p) 釋放該對象。
GNUstep <4>dealloc method
---------Apple’s Implementation of alloc, retain, release, and dealloc
CF/CFRuntime.c __CFDoExternRefOperation ?
Apple’s《1》--------runtime.m ? ?采用散列表(引用計數(shù)表) 表鍵值為內(nèi)存塊。
GNU《2》-------------Autorelease?
apple《2》-------------Autorelease
每個線程(包括主線程),都維護(hù)了一個管理 NSAutoreleasePool 的棧纠永。當(dāng)創(chuàng)先新的 Pool 時器予,他們會被添加到棧頂。當(dāng) Pool 被銷毀時折欠,他們會被從棧中移除。
autorelease 的對象會被添加到當(dāng)前線程的棧頂?shù)?Pool 中。當(dāng) Pool 被銷毀或颊,其中的對象也會被釋放。
當(dāng)線程結(jié)束時传于,所有的 Pool 被銷毀釋放囱挑。
AutoreleasePoolPage 對象存儲除了上面的實例變量所占空間autorelease對象的地址? next 指針指向下一個 add 進(jìn)來的 autorelease 的對象即將存放的位置thread 指針指向當(dāng)前線程。AutoreleasePoolPage 以雙向鏈表的形式組合而成(分別對應(yīng)結(jié)構(gòu)中的 parent 指針和 child 指針)一個 Page 的空間被占滿時沼溜,會新建一個 AutoreleasePoolPage 對象平挑,連接鏈表。
-[NSAutoreleasePool retain]: Cannot retain an autorelease pool' ? autoreleasePool自動釋放池不能被 retain只有一個對象指向它的地址
*** -[NSAutoreleasePool drain]: This pool has already been drained, do not release it (double release).提示系草,做了特別處理通熄。[pool0drain];之后不需要再被release了。 drain里面做了處理找都。
參考https://github.com/opensource-apple
http://svn.gna.org/svn/gnustep/libs/base/branches/dawn/Source/NSObject.m?
參考 GNUstep/modules/core/base/Source/NSObject.m alloc