小秋的家

          home

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            1 Posts :: 91 Stories :: 17 Comments :: 0 Trackbacks


          看了下官方文檔的關于object c 的內存管理,總結下:

          在iphone中object c 中的內存管理是通過計數器來管理的,每個對象內部都有一個計數器.新建一個對象,或者這個對象被其他對象引用,多會使計數器加1.Retain 增加計數器值release 減少計數器值.當計數器為0時對象就dealloc自己.
          在object c中你生成的一個對象那么你就有責任去釋放它,內存管理的一般規則:

          • You own any object you create by allocating memory for it or copying it.

            Related methods:alloc,allocWithZone:,copy,copyWithZone:,mutableCopy,mutableCopyWithZone:

          • If you are not the creator of an object, but want to ensure it stays in memory for you to use, you can express an ownership interest in it.

            Related method:retain

          • If you own an object, either by creating it or expressing an ownership interest, you are responsible for releasing it when you no longer need it.

            Related methods:release,autorelease

          • Conversely, if you are not the creator of an object and have not expressed an ownership interest, you mustnotrelease it.


          考慮下面這個例子:
          Thingamajig *thingamajig = [[Thingamajig alloc] init];
          NSArray *sprockets = [thingamajig sprockets];
          [thingamajig release];
          thingamajig 需要release,因為很顯然這里我們是thingamajig的owner。(第一條規則)
          那么sprockets為什么不需要release呢。
          首先我們不是sprockets的 creater.(第一條規則),我們也沒有expressing an ownership interest,因為我們沒有
          retain它,(第二條規則) 所以我們不負責release它。
          具體來看thingamajigsprockets方法的實現:
          (NSArray *)sprockets {
          NSArray *array;
          array = [[NSArray alloc] initWithObjects:mainSprocket,auxiliarySprocket, nil];
          return [array autorelease];
          }
          可見分配的內存在方法里就已經用autorelease釋放了,只不過是稍后釋放。參見autorelease pool。
          這里有個很重要的原則:
          Becausethe classcreates the new object,itis responsible for disposing of the new object.
          這個對象在哪里生成的,就應該在哪里釋放。
          所以你看到很多類有以自己類名為方法的構造函數,你調用的時候就不需要release.因為在這個構造函數中已經做
          autorelease了。
          你沒有alloc這個對象就不負責release它
          現在來看規則2,有些時候我們在自己的類里要引用其他的對象,如把這個其他的對象變成自己的成員變量。
          那么就有一個問題,這個被引用的對象是否被release不被你控制,一旦它被release,你自己的程序就會出問題
          這時候我們就需要retain這個對象。表示你這個類也擁有這個對象,只有你自己的類release后,被引用的對象才有可能被release
          一般的set 方法:
          @interface Counter : NSObject {
          NSNumber *count;
          }
          - (void)setCount:(NSNumber *)newCount {
          [newCount retain];
          [count release];
          count = newCount;
          }
          在set中我們對傳進來的對象進行了retain. 為什么要先retainrelease,因為如果newCountcount是同一個對象
          先release的話會導致這個對象delloc.
          posted on 2010-12-30 15:47 棋劍小秋 閱讀(560) 評論(0)  編輯  收藏 所屬分類: object-c

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 丹江口市| 蛟河市| 互助| 连城县| 靖宇县| 敦煌市| 岳池县| 新晃| 剑河县| 榆社县| 临颍县| 林西县| 将乐县| 德庆县| 贵港市| 万州区| 古蔺县| 泸定县| 岳池县| 汤阴县| 广南县| 昭觉县| 会昌县| 天镇县| 鹤岗市| 安康市| 雅安市| 铁力市| 凤庆县| 平度市| 军事| 永兴县| 芷江| 望江县| 左贡县| 河曲县| 安福县| 确山县| 合阳县| 瓦房店市| 伊宁市|