ã¡ã¢ãªç®¡ç
iPhoneéçºã§ãã¡ã¢ãªç®¡çã®åºç¤ã社å¡ã«ä¼ãããã¨ãå¢ãã¦ããã®ã§ãã¨ã³ããªã¨ãã¦æ¸ããã
Objective-Cåºç¤
ã¡ã¢ãªç®¡çã®åã«ObjCã®åºç¤ã¨ãã¦ãã¡ã½ããå¼ã³åºãã®è©±ã
ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹aãmethodAãã³ã¼ã«ããã¨ãã¯ã
[a methodA]
ã¨æ¸ãããã®ã¨ããaãnilã ã£ãã¨ãã¯ãã¨ã©ã¼ã§ã¯ãªããã³ã¼ã«ãããªããmethodAã«æ»ãå¤ãããã¨ãã¯ãããã¯ã0ãnilãNOãè¿ããObjCã§ã¯ã
void dealloc { ãif(a!=nil){ ãã[a release]; ã} ã[super dealloc]; }
ã¯ãæ°æã¡æªãã®ã§ãnilãã§ãã¯ã¯ããã¾ãããã
ãªããObjCã§ã¯ãåçã«ã¡ã½ãããå·®ãæ¿ãããã¨ãã§ããã³ã¼ã«ã®åº¦ã«ã¡ã½ãããåå¨ãã¦ãããã確èªãã¦ãã¾ãããã®ãããLLè¨èª(ã©ã¤ãã¦ã§ã¤ãè¨èªãã¹ã¯ãªããï¼ã®ããã«æè»ãªè¨è¿°ãå¯è½ã§ããããã¦ããã®ãããäºåã«å®£è¨ããã¦ããªãã¡ã½ããå¼ã³åºãã¯ã¨ã©ã¼ã¨ãªããè¦åã«ãªãã¾ãã
ã¡ã¢ãªç®¡çåºç¤
ãããããªè¨èªãããã¾ãããCè¨èªã§ã¯ãå
¨é¨èªåã§ã¡ã¢ãªç®¡çãã¾ãããJavaãC#ãLLè¨èªã§ã¯ã¬ã¼ãã¼ã¸ã³ã¬ã¯ã·ã§ã³ã§è§£æ¾ãã¾ããObjCã¯ãåç
§ã«ã¦ã³ã¿ã§ç®¡çãã¾ãï¼ObjC2.0ã®ã¬ã¼ãã¼ã¸ã³ã¬ã¯ã¿ã¯ä½¿ã£ããã¨ããªãï¼ãåç
§ã«ã¦ã³ã¿ã§ã®ç®¡çã¯ãå
¨é¨èªåã¨ã¬ã¼ãã¼ã¸ã³ã¬ã¯ã¿ã®éãããã®ç®¡çã§ãå
¨é¨èªåããã¯æ¥½ã¨è¨ã£ãã¨ããã§ããWindowsã®COMããªãã¡ã¬ã³ã¹ã«ã¦ã³ã¿ã§ããã
åç
§ã«ã¦ã³ã¿ã®ã¤ã³ã¯ãªã¡ã³ããã[a retain];ããã¯ãªã¡ã³ãã[a release];ãç¾å¨ã®ã«ã¦ã³ããå¾ãã®ã[a retainCount];ã§ãããããã¯NSObjectã®ã¡ã³ãã§ãããã¨ãNSAutorelasePoolããã£ã¦ãããã¯ã¹ã¬ãããã¨ã«æä½ï¼ã¤ã¯ã¤ã³ã¹ã¿ã³ã¹ãããã¾ãã
[a autorelease];ãã³ã¼ã«ããã¨ãã«ã¬ã³ãã®ãªã¼ããªãªã¼ã¹ãã¼ã«ã«ãã®ã¤ã³ã¹ã¿ã³ã¹ãå¾ã§release(åç
§ã«ã¦ã³ãã®ãã¯ãªã¡ã³ã)ããããã«ä¾é ¼ãã¾ããã¡ã¤ã³ã¹ã¬ããã®ã³ã¼ããæ¸ãã¨ãã¯ãã¤ãã³ãã®æåã«ãªã¼ããªãªã¼ã¹ãã¼ã«ãä½æãããã¤ãã³ãå¦çãçµããã¨autoreleaseä¾é ¼ããããã®ãreleaseãã¾ãããã®ãªã¼ããªãªã¼ã¹ãã¼ã«ã¯ãã¡ã¤ã³ã¹ã¬ããã®ã¤ãã³ãçµç±ã®å¦çã§ã¯ã·ã¹ãã å´ããã£ã¦ãããã®ã§ãã¢ããªã¯æ°ã«ããå¿
è¦ãããã¾ãããããã¯ã°ã©ã¦ã³ãã¹ã¬ãããä½æããã¨ãã¯ãã¹ã¬ããã®æåã§ãªã¼ããªãªã¼ã¹ãã¼ã«ãä½æãã¦ãã¹ã¬ããã®çµããã§è§£æ¾ããå¿
è¦ãããã¾ããåç
§ã«ã¦ã³ã¿ã0ã«ãªãã¨ãdeallocï¼ãã¹ãã©ã¯ã¿ï¼ãå¼ã³åºããã¾ãã
retain, releaseã®ã«ã¼ã«
ã§ããã¤retainãã¦ããã¤releaseãã¦ããã¤autoreleaseã使ã£ã¦ããã¤èªåã®æ¸ããã³ã¼ãã§ãªãã¨ããã§ã«ã¦ã³ãã¢ãããããã®ï¼ã¨ããã¨ããã§ããã
- ã¡ã³ãå¤æ°ã®ä»£å ¥ãã°ãã¼ãã«å¤æ°ã®ä»£å ¥ãstaticå¤æ°ã®ä»£å ¥ãªã©ãï¼ã¤ãã³ãå¦ç以éãåç §ãç¶ããå ´åã¯ãretainãããã°ãã¼ãã«å¤æ°ãstaticå¤æ°ã¯ã»ã¨ãã©ä½¿ããªãã ããããå®è³ªã¡ã³ãå¤æ°ã¸ã®ä»£å ¥ã§retainã¨è¦ãã¦ããã°è¯ãã§ããã
- ãã¼ã«ã«å¤æ°(autoå¤æ°)ã§ã¯ãretainãreleaseããªã
- allocãnewã§å§ã¾ãã¡ã½ãããCopyãå«ã¾ããã¡ã½ããã¯ãåãåãå´ãretainããã®ã§ãªãããã®ã¡ã½ããå ã§retainããã¦ãããï¼alloc以å¤ã¯ããã¾ã使ããªãã®ã§ãããã§ã¯allocã ãè¦ãã¦ããã°è¯ãã§ãããï¼
ã¨ããã®ãåºæ¬ã§ãã
çææ
ä»ã®ã¯ã©ã¹ã®çææã§ãããinitç³»ã®ã¡ã½ããã¨ã¯ã©ã¹åã§å§ã¾ãçæç³»ã®ã¡ã½ãããããã¾ããä¾ãã°ãNSDataã¯ã©ã¹ã«ã¯ããinitWithBytes:length:ãã¨ãdataWithBytes:length:ãã¨ã¨é常ã«ååã®ä¼¼ãã¡ã½ãããããã¾ãï¼initç³»ã¯ãããã©ãã¯ã©ã¹åã§å§ã¾ãç³»ããªãã¯ã©ã¹ãããã¾ãï¼ãinitç³»ã¯ã
NSData* data = [[[NSData alloc] initWithBytes:"ABC" length:3] autorelease];
ã®ãããã«ãã¯ã©ã¹åã§å§ã¾ãç³»ã¯ã
NSData* data = [NSData dataWithBytes:"ABC" length:3];
ã®ããã«æ¸ãã¾ããã©ã¡ãã§æ¸ãã¦ãåãçµæã§ãã¯ã©ã¹åã§å§ã¾ãçæç³»ã®æ¹ãè¨è¿°ãå°ãªãæ¸ã¿ã¾ããã§ãinitç³»ã¯ãallocæç¹ã§åç
§ã«ã¦ã³ã¿ãï¼ã§ãçæããã¾ããããã¦ãä¸è¨ä¾ã ã¨autoreleseãã¤ãã¦ããã®ã§ããã®ã¤ãã³ããçµãã£ããreleaseããã¾ãããã®autoreleaseãã¤ãã¦ããã¤ã³ã¹ã¿ã³ã¹ã¯ãä»retainCountãè¦ãã¨ï¼ã§ã¯ãããã®ã®ãªã¼ããªãªã¼ã¹ãã¼ã«ã«ç»é²ããã¦ããã®ã§ããã®ã¤ãã³ããçµãã£ãã¨ãã«ã¯ï¼ã«ãªãã®ã§ãã¤ãã³ããçµãã£ãã¨ããã¼ã¹ã§èããã¨ãåç
§ã«ã¦ã³ããï¼ã¨è¦ãªããã¨ãã§ãã¾ã(å®è³ª0)ãã¾ããdataWithBytesã®æ¹ãä¸èº«ã¯alloc init autorelaseããã£ã¦ãããã®ã¨æããã¾ããåã«è¨è¿°ãçããªãããã«ç¨æããã¦ããã ãã§ãã
ä¸è¨ä¾ã¯ããã¼ã«ã«å¤æ°ã¸ã®ä»£å
¥ã§ããããretainããå¿
è¦ãããã¾ãããä»ã®ã¤ãã³ãå¦çãçµãã£ããå
¨é¨è§£æ¾ããã¦æ¬²ããããã§ããã¤ã¾ãã代å
¥æ¼ç®åã®å·¦è¾ºãä¸æå¤æ°ï¼ã¤ã¾ãåç
§ãä¿æãç¶ããªãï¼ã§ãããããå³è¾ºãalloc init autoreleaseã§åç
§ã«ã¦ã³ããå®è³ªï¼ã«ãã¦ã左辺ã¨å³è¾ºããã©ã³ã¹ãããã¨ãããã¨ã§ãã
ãããã°ããã§ãã¡ã¢ãªã®æ¶è²»ãæ¼ããããå ´åã¯ã
NSData* data = [[NSData alloc] initWithBytes:"ABC" length:3]; //dataã使ã£ãå¦ç [data release];
ã¨è¨è¿°ãããã¨ãå¯è½ã§ãããã®å ´åã[data release]ã§åç §ã«ã¦ã³ãã0ã«ãªããªãå³ã¡ã¢ãªã解æ¾ããã¾ãï¼ãdataã使ã£ãå¦çãã§ãä»ã®ã¤ã³ã¹ã¿ã³ã¹ã«dataã渡ãã¦ããã®ã¤ã³ã¹ã¿ã³ã¹ãé·æä¿æãããªãretainãããã§ããããã解æ¾ãããªãï¼ããã ããçæè¡ã¨releaseãé¢ããã¨è§£æ¾å¿ããããããéçºãã¼ã ã®ä»ã®ã¡ã³ãã¼ããã®ã½ã¼ã¹ãè¦ãã¨ããã¡ãã£ã¨å±éºãªã³ã¼ããã¨ãã¦ãã¡ããã¨è§£æ¾ãã¦ããããã§ãã¯ãã¦ãã¾ãã®ã§ããå§ããã¾ããã
ã¡ã³ãå¤æ°ã¸ã®ä»£å ¥
ã¡ã³ãå¤æ°ãªã©ãï¼ã¤ã®ã¤ãã³ãå¦çãè¶ ãã¦åç §ãä¿æããå ´åã¯ãretainããå¿ è¦ãããã¾ããdataMemberãã¡ã³ãå¤æ°ã¨ãã¦ãçæã®ã¨ãã¯ã
[dataMember release]; dataMember=[[NSData alloc] initWithBytes:"ABC" length:3]; //allocå é¨ã§retainããã
ã¨æ¸ãã¾ããdataMemberãnilã¨åãã£ã¦ããã¨ãã¯ã[dataMember release]ã¯ä¸è¦ã§ãããããã§ãªãã¨ãã¯æ¸ãå¿
è¦ãããã¾ããåè¿°ã®éããdataMemberã®nilãã§ãã¯ã¯ä¸è¦ã§ãã
å¼æ°ã§ããã£ããaDataãã¡ã³ãå¤æ°ã«ä»£å
¥ããã¨ãã¯ã
[aData retain]; [dataMember release]; dataMember=aData;
ã¨æ¸ãã¾ããnilãä»£å ¥ãããã¨ãã¯ã
[dataMember release]; dataMember=nil;
ã¨æ¸ãã¾ããnilã®ä»£å
¥ãå¿ããã¨ï¼é解æ¾ã®åå ã«ãªãã®ã§dealloc以å¤ã§ã¯nil代å
¥ãå¿ããªãããã«ãããã¨ã
ãªãã誰ãã®ä½ã£ãã¡ã½ããã§ããã¯ã©ã¹ã®åç
§ãå¾ãããã¨ãããã®ã¡ã½ããå´ã§åç
§ã«ã¦ã³ã¿ãåæã«ããããã¨ã¯ããã¾ããï¼allocãããã®ä»newãcopyãã¡ã½ããåã«ã¤ããã®ãªã©ä»¥å¤ï¼ãä¾ãã°ãèªåã®ãã¥ã¼ã®è¦ªãã¥ã¼ãå¾ãã®ã«
UIView* parent=[self superview];
ã¨å¼ã³åºãã¦ãåæã«ã¯åç §ã«ã¦ã³ãã¯å¢ãã¾ãããåç §ã«ã¦ã³ããå¢ãã責任ãããã®ã¯ãä»£å ¥ãæ¸ã人ã§ããéã«èªåã§ã¡ã³ãå¤æ°ãè¿ãã³ã¼ããæ¸ãã¨ããretainãã¦è¿ãã¦ã¯ããã¾ããã
-(NSData*)getDataMember { ã[dataMember retain];//NG! ãreturn dataMember; }
ã¨æ¸ãã¦ã¯ããã¾ãããä½ãçæãã¦è¿ãã¡ã½ãããæ¸ãã¨ããautoreleaseãã¤ãã¦å®è³ª0ï¼ä»ã®ã¤ãã³ãå¦çå¾ã§0ï¼ã§è¿ãã¹ãã§ãã
-(NSData*)getData { ãreturn [[[NSData alloc] initWithBytes:"ABC" length:3] autorelease]; }
ä¸è¨ã¯ãallocå é¨ã§åç §ã«ã¦ã³ããï¼ã§çæããã¦ãautoreleaseã§å®è³ªï¼ã¨ãªãã¾ãã
ããããã£ãã¬ã³ã¬ã³ä½¿ãã
ãã¦ãä½åº¦ãretainã¨releaseãæ¸ãä¾ãã§ã¦ãã¾ãããretainæ°ã«æ¯ã¹ã¦releaseãå°ãªãã¨ã¡ã¢ãªãªã¼ã¯ã«ãªãããretainæ°ã«æ¯ã¹ã¦releaseãå¤ãã¨releaseæã«
malloc: *** error for object 0x4c40740: pointer being freed was not allocated
ã¨ã³ã³ã½ã¼ã«ã«ã¨ã©ã¼ãåãã¦ã¯ã©ãã·ã¥ãã¾ããinitãã¦autoreleaseãã³ã¼ã«ãã¦ãreleaseãããå ´åã¯ããªã¼ããªãªã¼ã¹ãã¼ã«ã®è§£æ¾å¦çã§ã¯ã©ãã·ã¥ãã¾ããå½ããåã§ãããretainã®ã³ã¼ã«ãããæ°ã¨releaseã®ã³ã¼ã«ãããæ°ã¯å³å¯ã«ããããã¦ããªããã°ãªãããããã°ã©ããæ
éã«ç®¡çããªããã°ãªãã¾ãããããã§ãretainã¨releaseã極åæ¸ããªãã§æ¸ãããã«ãããã¨ããå§ããã¾ãã
ãã®çºã«ããããã£ã使ãã¾ããããããã£ã¯ãããããã¡ã¤ã«ã«
@interface Person { ãNSString* name; } @property (nonatomic, retain) NSString* name; @end
ã¨æ¸ãã¦ãå®è£ ãã¡ã¤ã«(*.mã*.mm)ã«
@implementation Person @synthesize name; //ä¸èº« @end
ã¨æ¸ãã¾ãã@propertyã®nonatomicå±æ§ã¯ãã¹ã¬ããã»ã¼ãã§ãªããé«éã¨ããå±æ§ã§ãããã«ãã¹ã¬ããã§è¤æ°ã®ã¹ã¬ããããã¢ã¯ã»ã¹ãããªãã¨ãã¯ãnonaotmicã§ããã§ããããretainå±æ§ã¯ãèªåã§retainãreleaseããã¦ããããã®ã§ãããªããnonatomicãretainãæ¸ãã®ã¯ãã¯ã©ã¹ã®ãã¤ã³ã¿åã®ã¨ãã§ãããããªããã£ãåã®ã¨ãã¯ã@property int type;ã®ããã«ãå±æ§ã¯ä¸è¦ã§ãã
å®è£
ãã¡ã¤ã«ã«æ¸ã@synthesizeã¯ãgetterã¨setterãèªåã§æ¸ãã¦ããããã®ã§ãã
ã§ããã®setterã§ãããèªåã§ã
-(void)setName:(NSString*)a { ã[a retain]; ã[name release]; ãname = a; }
ã¨ãããã®ãèªåã§ä½ã£ã¦ããã¾ãï¼è¦ããªããã©ãä¸è¨ã¨åçã®ã³ã¼ãã«å±éãããã¨æãããï¼ã
ã§ãsetterã®å¼ã³åºãã¯ã
self.name = aName;
ã¿ããã«æ¸ãã¨setterãå¼ã³åºããã¾ããããããã£ã¨ãã¦å®£è¨ãã¦ã
name = aName;
ã¨æ¸ãã¨ç´æ¥ã¡ã³ãå¤æ°ã¸ã®ä»£å ¥ã¨ãªããsetterãå¼ã°ãã¾ããã
- self.nameã¨nameã¯å ¨ç¶éãï¼
ã¨ãããã¨ã§ããããã¦å ¨ã¦ã®ãã¤ã³ã¿åã®ã¡ã³ãå¤æ°ãããããã£ã«ãããã¨ã§ãã»ã¨ãã©å ¨ã¦ã®retainãreleaseãæ¸ããã«æ¸ã¿ã¾ããä¸ã«æããä¾ãæ¸ãç´ãã¨ãçæãã¦ã¡ã³ãå¤æ°ã«ä»£å ¥ãªã
self.dataMember = [NSData dataWithBytes:"ABC" length:3]; //ä»£å ¥åã®åç §ã«å¯¾ãã¦releaseãã¦ããã
[dataMember release]ãããã«ä»£å ¥ã§OKã«ãªããã¯ã©ã¹åã§å§ã¾ãã¡ã½ããããªãã¨ãã¯ã
self.dataMember = [[[NSData initWithBytes:"ABC" length:3] autorelease];
[dataMember release]ãããã«ä»£å ¥ã§OKã«ãªããå¼æ°ã§æ¥ãaDataãã¡ã³ãå¤æ°ã«ä»£å ¥ãªãã
self.dataMember = aData; //ä»£å ¥åã®åç §ã«å¯¾ãã¦releaseããããä»£å ¥å¾ã®åç §ã«å¯¾ãã¦retainããã
releaseãretainãä¸è¦ãnilã®ä»£å ¥ã
self.dataMember = nil;//releaseããã
ã§OKã§ããããã§ã»ã¼ãã¹ã¦ã®å ´æã§retainã¨releaseã使ããã«æ¸ã¿ã¾ãããã æ £ç¿ã¨ãã¦ãdeallocã¯ãnilä»£å ¥ã§ãªãreleaseãå¼ã¶ãã¨ãå¤ãããã§ããããããããããã£ã«å¯¾ãã¦nilä»£å ¥ã¨ããã³ã¼ãã£ã³ã°ã«ã¼ã«ã«ããã°releaseãªãã«ã§ããã§ãããã
- ãã¤ã³ã¿åã®ã¡ã³ãå¤æ°ããã¹ã¦ããããã£ã«ããã°retainã¨releaseã¯ãååå¼ã°ãªãã¦ãããªã(ä¾å¤ã¯ãããã©ãsetterã®ä¸ã¨ãããã«ãã¹ã¬ããã¨ããï¼
çæã¨ãã£ããã«ä»äººã«ã»ãã
ã©ã®ã¡ã½ããå¼ã³åºãã§åç §ã«ã¦ã³ããã¢ããããã¦ããã¤è§£æ¾ããããæ°ã«ãªã人ããã¾ããããã¡ã³ãå¤æ°ï¼ãªã©é·æã«ä¿æããå¤æ°ï¼ã«ä»£å ¥ãã人ãã«ã¦ã³ãã¢ããããã¨ããã®ãååãªã®ã§ãæ®éãå¼ã³åºãå´ã¯æ°ã«ããå¿ è¦ãããã¾ããï¼deallocãå¼ã°ããã¿ã¤ãã³ã°ãéè¦ã¨ããã«ãã¹ã¬ããã§ãªãéãï¼ã
view.backgroundColor=[[UIColor alloc] initWithRed:0.5f green:0.3f blue:0.1f alpha:1.0f];
ããã¯ééãã§ãããªããªãããã¯ã
UIColor* color = [[UIColor alloc] initWithRed:0.5f green:0.3f blue:0.1f alpha:1.0f]; view.backgroundColor=color;
ã¨åãã ããã§ãããã¼ã«ã«å¤æ°ã®ä»£å ¥ã¯ãalloc init autoreleaseãã»ããã§ä½¿ãããã¯ã©ã¹åã§å§ã¾ãã³ã³ã¹ãã©ã¯ã¿ç³»ã使ããã§ãï¼å¥éãreleaseãæ示çã«å¼ã¶ã®ãå¯ã§ãããããã§ã¯retainã¨releaseãã§ããã ã使ããªãããã«ãããã¨ãã趣æ¨ãªã®ã§ï¼ãæ£ããã¯ã
view.backgroundColor=[UIColor colorWithRed:0.5f green:0.3f blue:0.1f alpha:1.0f];
ã¨ãªãã¾ãã
ãã®çæãããã«ã©ã¼ã¯ãviewããããåç
§ããªããã°ï¼æ®éããï¼ãviewã®è§£æ¾å¦çã¨ã¨ãã«è§£æ¾ããã¾ãã
privateãªããããã£
ã¡ã³ãå¤æ°ãããããã£ã«ããã¨ãä»ã®ã¯ã©ã¹ããæ¸ãæããåç §ãæ¾é¡ã«ãªãpublicã«ãªã£ã¦ãã¾ãã¾ããretain, releaseæå°åã®ããã«ããããã£ã«ãããããå ¬éããããªãå ´åã¯ãã¯ã©ã¹ã®æ¡å¼µã使ã£ã¦ãããããã¡ã¤ã«ã«æ¸ããã«å®è£ ãã¡ã¤ã«ã«æ¸ãã¾ããå ã»ã©ã®Personã¯ã©ã¹ã®nameã¡ã³ãå¤æ°ã®ä¾ã§è¡ãã¨ã
//Person.h @interface Person { ãNSString* name; } @end
ããããã¡ã¤ã«ã«ã¯ã@propertyãæ¸ãã¾ãããããã¦å®è£ ãã¡ã¤ã«ã®ä¸ã®æ¹ã«
@interface Person () //ã¯ã©ã¹æ¡å¼µ @property (nonatomic, retain) NSString* name; -(void)somePrivateMethod; @end @implementation @synthesize name; //å 容 @end
ã®ããã«ãPersonãæ¡å¼µãããã¨ã§ãä»ã®ã¯ã©ã¹ããè¦ããªãããã«ãã¾ãããã®æ¡å¼µã使ããã¨ã§ãprivateã¡ã½ãããä½ãã¾ããï¼ãã ããã«ãæã«è¦ããªãã ãã§ãå®è¡æã«ç¡çç¢çå¼ã³åºãã°å¼ã³åºããã§ãããï¼
èªåã§NSAutoreleasePool
ãã¾ã«èªåã§ãªã¼ããªãªã¼ã¹ãã¼ã«ã®ä½æããããã¨ãããã¾ããï¼ã¤ã¯åè¿°ããå¥ã¹ã¬ããã使ãã¨ãã§ããã¹ã¬ããã¡ã¤ã³ã®æåã«
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
ãæ¸ãã¦ãã®ã¹ã¬ãããçµããã¨ãã«
[pool release];
ãæ¸ãã ãã§ãããã®ä»ã«ãã¡ã¤ã³ã¹ã¬ããã§ããµãã¹ã¬ããã§ã大éã«ãªã¼ããªãªã¼ã¹ãã¼ã«ã«ç»é²ãããããªå ´åã§ãã
for(int i=0;i<10000;i++){ ãNSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; ãNSString* a=[NSString stringWithFormat:@"%@=%d", b, c]; ã//é©å½ãªé·ãå¦ç ã[pool release]; }
ãã®å ´åãstringWithFormatã®ã³ã¼ã«ã§ã¬ã³ã¬ã³ã¡ã¢ãªãä½ãããã®ã§ãããèªåã§ãªã¼ããªãªã¼ã¹ãã¼ã«ãä½ããªãã¨ãã«ã¼ãä¸ã«è§£æ¾ãããªãã®ã§ã¡ã¢ãªãã©ãã©ãæ¶è²»ãã¾ããã«ã¼ãã®ä¸ã«ãªã¼ããªãªã¼ã¹ãã¼ã«ãä½ããã¨ã§ãã«ã¼ãã®ï¼åãã¨ã«è§£æ¾ããã¦ã¡ã¢ãªä½¿ç¨éãæãããã¨ãã§ãã¾ãã
ã¡ã¢ãªãªã¼ã¯ã®è©¦é¨
Xcodeã§ã¯ãç°¡åã«ã¡ã¢ãªãªã¼ã¯ã®è©¦é¨ãã§ãã¾ããã¾ããiOS4ãããã¼ã ãã¿ã³ãããã¦ãããã¯ã°ã©ã¦ã³ãã§ãµã¹ãã³ããããã¾ã¾ã«ãããã¨ãã§ããããã«ãªã£ãããããããONã ã¨è©¦é¨ã«ãªããªãã®ã§ãOFFã«ãã¾ããInfo.plistã§ããApplication does not run in backgroundããONã«ãã¾ããããã¦ãããã°ãã«ããã¾ãã
ã¡ãã¥ã¼ï¼å®è¡ï¼ããã©ã¼ãã³ã¹ãã¼ã«ã使ã£ã¦å®è¡ï¼Leaks
ã§å®è¡ãã¾ããInstrumentsãèµ·åãããã®ã§ããã®ç¶æ
ã§é©å½ã«æä½ãã¦ãã¼ã ãã¿ã³ãæ¼ãã¾ãããªã¼ã¯ãã¦ããå ´åã¯ããªã¼ã¯ãããã¯ãåºã¾ãã
ä¸ã®ãã¤ã³ã«Allocationsã¨Leaksã®ï¼ã¤ã®ã°ã©ããåºãã®ã§Leaksãã¯ãªãã¯ãã¦ã«ã¬ã³ãã«ãã¾ããä¸ãã¤ã³ã¨ä¸ãã¤ã³ã®éã«ãLeaked Blocksãã¨ãããã«ãã¦ã³ãããã®ã§ãããã§Call Treeãé¸æãã¾ããããã®ããªã¼ãå±éãã¦ããã¨èªåã®ã³ã¼ãã§initãnewãmallocããè¡ãåºã¾ããã½ã¼ã¹ã³ã¼ãã表示ãããnewããè¡ããã¤ã©ã¤ãããã¾ããã¡ã¢ãªã確ä¿ããä½ç½®ãåããã°ã大æµããã«ä¿®æ£ã§ããã§ãããã
ã¾ã¨ãï¼ãã¿ããå¼ retain,releaseããªãã³ã¼ãã£ã³ã°ã«ã¼ã«
ã«ã¼ã«ãã¾ã¨ããã¨
- ã«ã¼ã«ï¼ï¼ã¯ã©ã¹ã®ãã¤ã³ã¿ç³»ã®ã¡ã³ãå¤æ°ã¯ããã¹ã¦retainå±æ§ä»ãã®ããããã£ã«ãããå¿ è¦ãªãã°ã¯ã©ã¹æ¡å¼µã使ã£ã¦privateã«ã
- ã«ã¼ã«ï¼ï¼ã¯ã©ã¹ã®ãã¤ã³ã¿ç³»ã®ã¡ã³ãå¤æ°ã¸ã®ä»£å ¥ã¯ãå¿ ãself.ãã¤ãã¦ããããã£çµç±ã§ä»£å ¥ããã
- ã«ã¼ã«ï¼ï¼ã¤ã³ã¹ã¿ã³ã¹ã®çæã¯ãå¿ ãalloc init autoreleaseã¾ãã¯ãã¯ã©ã¹åã§å§ã¾ããã¡ã¯ããªã¡ã½ããã使ãã左辺ãã¡ã³ãå¤æ°ã®ã¨ãã¯ãself.ãã¤ãã¦ããããã£ã¸ã®ä»£å ¥ã«ããã
- ã«ã¼ã«ï¼ï¼retainã¯ä½¿ããªããreleaseã¯ãdeallocå ã¨NSAutoreleasePoolã®release以å¤ã¯ä½¿ããªãããã以å¤ã®å ´æã§åç §ã«ã¦ã³ããæ¸ããããã¨ãã¯ãself.ããããã£=nil;ã¨ããã
ã¨ããæãããã«ãã¹ã¬ããã¨ããã以å¤ã®ç¹æ®ãªå ´å(循ç°åç
§ã®åé¡ã¨ããå¾®å¦ãªã¿ã¤ãã³ã°ã®åé¡ã¨ãï¼ã§ãªããã°retain, releaseã¯ã»ã¼ãªãã«ã§ãã¾ãã
ããããã£ã使ã£ã¦ retainãreleaseãæå°ã«ãã¾ãããï¼
#2011/2/20 18:14 Cï¼ï¼ã®deleteåã®NULLãã§ãã¯ã®æç« ãã«ãããã¾ãããææãããã¨ããããã¾ãã
#2011/2/20 18:30 NSAutoreleasePoolã®ç« ã追è¨ãã¾ãã
#2011/2/20 19:16 ã«ãã´ãªã使ããæ¡å¼µã¨ãã¾ãã
#2011/2/20 21:03 çæã¨ãã£ããã«ä»äººã«ã»ããç¯ã追å
#2011/2/20 22:22 ææãããã¨ããããã¾ããC++ãã«ãã
#2011/2/20 22:41 ææãããã¨ããããã¾ããretain->release->代å
¥ã®é çªã«ä¿®æ£ãã¾ãã
#2011/3/27 ã¾ã¨ãç¯ã追å ãã¾ããã
ã¼ã¼
URL:ibisMailã®ãã¦ã³ãã¼ããã¬ãã¥ã¼ã¯こちら
Twitter: @kamiyan