ï»??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲欧洲一区二区,中文字幕一区二区三区在线播放,亚洲精品字幕http://www.aygfsteel.com/mycolby/category/47485.htmlhomezh-cnFri, 31 Dec 2010 11:15:26 GMTFri, 31 Dec 2010 11:15:26 GMT60object c 内存½Ž¡ç†(转蝲)http://www.aygfsteel.com/mycolby/articles/341995.html‹‚‹å‰‘ž®ç§‹‹‚‹å‰‘ž®ç§‹Thu, 30 Dec 2010 07:47:00 GMThttp://www.aygfsteel.com/mycolby/articles/341995.htmlhttp://www.aygfsteel.com/mycolby/comments/341995.htmlhttp://www.aygfsteel.com/mycolby/articles/341995.html#Feedback0http://www.aygfsteel.com/mycolby/comments/commentRss/341995.htmlhttp://www.aygfsteel.com/mycolby/services/trackbacks/341995.html

看了(ji¨£n)下官æ–ÒŽ(gu¨©)–‡æ¡£çš„关于object c 的内存管ç?æ€È»“ä¸?

在iphone中object c 中的内存½Ž¡ç†æ˜¯é€šè¿‡è®¡æ•°å™¨æ¥½Ž¡ç†çš?每个对象内部都有一个计数器.新徏一个对è±?或者这个对象被其他对象引用,多会(x¨¬)使计数器åŠ?.Retain 增加计数器å€?/span>release 减少计数器å€?当计数器ä¸?时对象就dealloc自己.
在object c中你生成的一个对象那么你ž®±æœ‰è´£ä“Q去释攑֮ƒåQŒå†…存管理的一般规则:(x¨¬)

  • 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.


考虑下面˜q™ä¸ªä¾‹å­åQ?/div>
Thingamajig *thingamajig = [[Thingamajig alloc] init];
NSArray *sprockets = [thingamajig sprockets];
[thingamajig release];
thingamajig 需要releaseåQŒå› ä¸ºå¾ˆæ˜„¡„¶˜q™é‡Œæˆ‘们æ˜?span style="font-family: 'Courier New';">thingamajigçš„owner。(½W¬ä¸€æ¡è§„则)(j¨ª)
那么sprocketsä¸ÞZ»€ä¹ˆä¸éœ€è¦?span style="font-family: 'Courier New';">releaseå‘¢ã€?br /> 首先我们不是sprocketsçš?creater.åQˆç¬¬ä¸€æ¡è§„则)(j¨ª),我们也没有expressing an ownership intereståQŒå› ä¸ºæˆ‘们没æœ?/span>
retain它,åQˆç¬¬äºŒæ¡è§„则åQ?所以我们不负责release它ã€?/span>
具体来看thingamajig�span style="font-family: 'Courier New';">sprockets
æ–ÒŽ(gu¨©)³•的实玎ͼš(x¨¬)
(NSArray *)sprockets {
NSArray *array;
array = [[NSArray alloc] initWithObjects:mainSprocket,auxiliarySprocket, nil];
return [array autorelease];
}
可见分配的内存在æ–ÒŽ(gu¨©)³•里就已经ç”?span style="font-family: 'Courier New';">autorelease释放äº?ji¨£n),只不˜q‡æ˜¯½EåŽé‡Šæ”¾ã€‚参见autorelease poolã€?/span>
˜q™é‡Œæœ‰ä¸ªå¾ˆé‡è¦çš„原则åQ?/strong>
Becausethe classcreates the new object,itis responsible for disposing of the new object.
˜q™ä¸ªå¯¹è±¡åœ¨å“ªé‡Œç”Ÿæˆçš„åQŒå°±åº”该在哪里释放ã€?/strong>
所以你看到很多¾cÀLœ‰ä»¥è‡ªå·Þq±»åäØ“(f¨´)æ–ÒŽ(gu¨©)³•的构造函敎ͼŒä½ è°ƒç”¨çš„æ—¶å€™å°±ä¸éœ€è¦release.因䨓(f¨´)在这个构造函æ•îC¸­å·²ç»å?/span>
˜q?/span>autoreleaseäº?ji¨£n)ã€?/span>
你没有alloc˜q™ä¸ªå¯¹è±¡ž®×ƒ¸è´Ÿè´£releaseå®?/span>
现在来看规则2åQŒæœ‰äº›æ—¶å€™æˆ‘们在自己的类里要引用其他的对象,如把˜q™ä¸ªå…¶ä»–的对象变成自å·Þqš„æˆå‘˜å˜é‡ã€?/div>
那么ž®±æœ‰ä¸€ä¸ªé—®é¢˜ï¼Œ˜q™ä¸ªè¢«å¼•用的对象是否被release不被你控åˆÓž¼Œä¸€æ—¦å®ƒè¢«releaseåQŒä½ è‡ªå·±çš„程序就ä¼?x¨¬)出问é¢?/div>
˜q™æ—¶å€™æˆ‘们就需要retain˜q™ä¸ªå¯¹è±¡ã€‚表½CÞZ½ ˜q™ä¸ª¾cÖM¹Ÿæ‹¥æœ‰˜q™ä¸ªå¯¹è±¡åQŒåªæœ‰ä½ è‡ªå·±çš„ç±»release后,被引用的对象才有可能被release
一般的set æ–ÒŽ(gu¨©)³•åQ?/div>
@interface Counter : NSObject {
NSNumber *count;
}
- (void)setCount:(NSNumber *)newCount {
[newCount retain];
[count release];
count = newCount;
}
在set中我们对传进来的对象˜q›è¡Œäº?ji¨£n)retain. ä¸ÞZ»€ä¹ˆè¦å…?span style="font-family: 'Courier New';">retainå?span style="font-family: 'Courier New';">releaseåQŒå› ä¸ºå¦‚æž?span style="font-family: 'Courier New';">newCountå’?span style="font-family: 'Courier New';">count是同一个对è±?/div>
å…ˆrelease的话ä¼?x¨¬)导致这个对象delloc.


]]> Ö÷Õ¾Ö©Öë³ØÄ£°å£º È«ÖÝÏØ| ¶î¶û¹ÅÄÉÊÐ| ¼ÃÄÏÊÐ| è÷äüÏØ| ƽÀÖÏØ| ãÉÐÐÇø| Ö¾µ¤ÏØ| ÅìºþÏØ| ºÚÉ½ÏØ| ÕżҸÛÊÐ| ÀÉÏªÏØ| ´¾°²ÏØ| ·ïÉ½ÏØ| ÇúÎÖÏØ| ÓÀºÍÏØ| »á¶«ÏØ| °¢ºÏÆæÏØ| ÎäÄþÏØ| ͨ»¯ÊÐ| ´óÒ¦ÏØ| ÁèÔÆÏØ| ÏåÔ«ÏØ| ÐÂÉÛÏØ| ¿ª·âÊÐ| ÂÞ¶¨ÊÐ| ÇåË®ÏØ| µÂ°²ÏØ| ´ó¹ØÏØ| ¹ÌÔ­ÊÐ| Ö¾µ¤ÏØ| ÇåÔ­| ÖÛÇúÏØ| ÉÏË¼ÏØ| ÓÀ´¨ÊÐ| ¸»´¨| Ñ®ÒØÏØ| ¹ãÄÏÏØ| ÙÙÖÝÊÐ| ÒÇÂ¤ÏØ| Õ´»¯ÏØ| µ¤Õ¯ÏØ|