iPhoneã¢ããªéçºã®ã¡ã¢ãªç®¡ç
æ¨æ¥ã¯ã¡ã¢ãªãªã¼ã¯ã¨æ¦ã£ã¦ãã¦ï¼èªåãè²ã
åéãããã¦ãããã¨ã«æ°ãã¤ããï¼
retainï¼autorelease,releaseé£ãããï¼ï¼
åæ
ããããï¼obj-cã§ã¯å¤æ°ã¯åç
§ã§ããæãã¦ãããã®ã¯ãªãã¸ã§ã¯ãã¨èããï¼
ãªãã¸ã§ã¯ããçæãããã³ã«ï¼cã§è¨ãã¨ããã®mallocã§åçã«ã¡ã¢ãªã確ä¿ãã¦ããã«å¤æ°ãå²ãå½ã¦ã¦ããç¶æ
ã«ãªãã®ã§ã¦ã¼ã¶ã¼ããã¡ãã¨ç®¡çï¼è§£æ¾ãè¡ããªãã¨ããã«ã¡ã¢ãªãªã¼ã¯ããï¼
ãã®ã¡ã¢ãªç®¡çãããè
ã ï¼
retainCountãæèãã
ãªãã¸ã§ã¯ãã¯ããããretainCountã¨ããå¤æ°ããã£ã¦ããï¼
obj = [[Class alloc] init];
ã¦ãªæãã«ãªãã¸ã§ã¯ããçæããã¨ãã«ï¼ã«ã¦ã³ãã1å¢ããï¼
ãã®ã«ã¦ã³ãã0ã«ãªã£ãã¨ãï¼ãªãã¸ã§ã¯ãã¯è§£æ¾ãããï¼
ï¼è§£æ¾ãããã¨ãï¼objããã£ã¦ããdeallocã¡ã½ãããå¼ã°ããï¼ï¼
ä¸è¨ã®ããã«çæããå ´åï¼retainCountã¯åæã«æ¸ããªãã®ã§ãã®ã¾ã¾ã ã¨ãã¤ã¾ã§ãã£ã¦ããªãã¸ã§ã¯ãã解æ¾ãããªãï¼
ã§ã¯ï¼ã«ã¦ã³ãã¯ã©ããã£ã¦å¢æ¸ããã®ãã¨ããã¨ï¼retain,releaseã«ãã£ã¦å¢æ¸ããï¼
release
[obj retain]; //retainCountãä¸ãã [obj release]; //retainCountãä¸ãã
ã¤ã¾ãï¼
obj = [[Class alloc] init]; [obj release]; //retainCountãä¸ãã
ã£ã¦ãªãã¨ãããã¨å³è§£æ¾ããï¼
é¢æ°ãªã©ã§ä¸æçãªãªãã¸ã§ã¯ããä½ãã¨ãã¯ï¼ä½¿ãçµãã£ããreleaseãã¦è§£æ¾ãã¦ãããï¼
大ä½ï¼alloc]init]ãåºã¦ãããreleaseã¯ã»ããã«ãªãï¼
ã¾ãï¼
NSString * str = [[NSString alloc] initWithString:@"hoge"];
ã¿ãããªinitãªãããããããã¨ããã¡ã½ããã§çæãããªãã¸ã§ã¯ããreleaseããå¿
è¦ãããï¼
éã«ï¼
NSString * str = [[NSString alloc] stringWithString:@"hoge"];
ã®ããã«initãé ã«ã¤ãã¦ãªãã¡ã½ããã§çæãããªãã¸ã§ã¯ãã¯å¾è¿°ããautoreleaseãé©ç¨ããã¦ãããããã®ã§releaseããå¿
è¦ã¯ç¡ãï¼
ãããæ©ãï¼
releaseããªãããããªãã®ãï¼ããå¿
è¦ç¡ãã®ãå¤å¥ã¯ååã ããªã®ãã¨ï¼
試è¡é¯èª¤ã®çµæï¼initãé ã«ã¤ãã¦ããªãã¡ã½ããã¯åºæ¬çã«ã¯releaseããªãã¦è¯ãããã§ãï¼
ããå¿é
ãªã
- ã¨ããããreleaseãã¦è½ã¡ãï¼
- 次ã«releaseæãã³ã¡ã³ãã¢ã¦ããã¦è½ã¡ãªãï¼
ãªã大ä¸å¤«ãããªãã§ããããï¼è²¬ä»»ã¯æã¦ã¾ãããï¼
retain
ãããï¼retainã¯ã©ãã§ä½¿ããã¨ããã¨ï¼ãããé£ããï¼
ä¾ãã°ï¼çæããobj_aãä»ã®ãªãã¸ã§ã¯ãobj_bã«ãããããã¨ãï¼
çæå
ã§obj_aãreleaseãã¦è§£æ¾ãããã¨ï¼obj_bã«ããããobj_aã®åç
§å
ã解æ¾ããã¦ããããï¼obj_bãå
é¨ã§obj_aã触ãã¨å¤åè½ã¡ãï¼
ãããåé¿ããããã«ï¼obj_bã[obj_a retain]ããã¦åç
§ã«ã¦ã³ããä¸ãã¦ããï¼
ããã¨ï¼çæå
ã§obj_aãreleaseãã¦ãobj_aã¯è§£æ¾ãããªãï¼
ãã ãï¼ä»åº¦ã¯obj_bããã¡ãã¨releaseããã¦è§£æ¾ãã¦ãããå¿
è¦ãããï¼
retainã¯ééã£ã使ãæ¹ãããã¨retainCountã0ã«ãªããªããªã£ã¦ãã¾ãã¡ã¢ãªãªã¼ã¯ã«ã¤ãªããã®ã§æ³¨æãå¿
è¦ï¼
retainãããreleaseãã©ããã§å¿
è¦ã«ãªãã®ãåºæ¬ï¼
autorelease
æåã§ãã¡ãã¡releaseããã®ã¯é¢åãªã®ã§ï¼autoreleaseã¨ããæ©æ§ãåãã£ã¦ããï¼
obj = [[[Class alloc] init] autorelease];
ãããã¦ãããã¨ã§ï¼objãçæãããã¡ã½ãããæãã¦é©å½ãªã¿ã¤ãã³ã°ã§releaseãåæã«èµ°ãï¼
ããã§retainCountã0ã«ãªãã°ãªãã¸ã§ã¯ãã¯è§£æ¾ãããï¼
便å©ï¼
ã¡ãªã¿ã«ã¢ããã«ããã¯iPhoneã®éçºã§ã¯ãã¾ãautoreleaseã使ããã¨ãæ¨å¥¨ãã¦ããªãã¿ããã§ãï¼
ã¡ã¢ãªç®¡çã®æ³¨æ
autoreleaseãèµ°ãã¿ã¤ãã³ã°ãè²ã
調ã¹ããï¼ã¤ãã³ãã«ã¼ããä¸åãããã¨ãã«èµ°ããï¼ã¨ãè²ã
æ
å ±ãè½ã¡ã¦ããã®ã ãï¼åºæ¬çã«ã¯ãªãã¸ã§ã¯ãã«autoreleaseãé©ç¨ããã¡ã½ãããæããããã¤çºçãã¦ãè¯ãããã«ãã¦ããå¿
è¦ãããã¨æãï¼
èªåã®å ´åã¯ï¼UITableViewControllerã®æåã«ä½ã£ããªãã¸ã§ã¯ãã«autoreleaseãé©ç¨ãããï¼ãã®å¾ã«å®è¡ãããnumberOfRowsInSectionã¾ã§ã¯cellForRowAtIndexPathãå®è¡ãããåã«autoreleaseãèµ°ã£ã¦ããï¼
ã¾ãï¼ã°ãã¼ãã«ã«è§¦ããã¯ã©ã¹å¤æ°ã«ãªãã¸ã§ã¯ããå ¥ãã¦ï¼è¤æ°ã¡ã½ããã§ä½¿ãåãå ´åï¼retainCountãææ¡ãã¦ããªãã¨
- ãã¤ã®éã«ããªãã¸ã§ã¯ãã解æ¾ããã¦ãã¦è½ã¡ã
- ãªãã¸ã§ã¯ãã解æ¾ããã¦ããªãã®ã«æ°ãããªãã¸ã§ã¯ããä½ã£ã¦ãã¾ãã¡ã¢ãªãªã¼ã¯ãã
ã®2ã¤ã«ããã«é¥ãã¾ãï¼
è²ã ãªãã¯ããã¯
ãã¯ããã¯ã¨ããã®ããããã¾ããã®ã§ããï¼é ã沸騰ããã¦è©¦è¡é¯èª¤ããã¦ããã¨ãã«ä½¿ã£ãæ¹æ³ã§ãï¼
NSZombieEnabled
retainCountã0ã«ãªã£ã¦è§£æ¾ããããªãã¸ã§ã¯ãã«è§¦ãã¨ï¼EXC_BAD_ACCESSãçºè¡ããã¦è½ã¡ãï¼
EXC_BAD_ACCESSã¨ãè¨ããã¦ãã©ãã§ãªã«ãèµ·ããã®ããã£ã±ãããããªãï¼
ããã§ï¼NSZombieEnabledãã¤ããã¨ããï¼
ããã¯ï¼こちらのサイトに詳しく書いてありますが,解æ¾ããããªãã¸ã§ã¯ãã«NSZombieã¨ããç©é¨ãªååã®ãªãã¸ã§ã¯ããå
¥ãã¾ãï¼
ãã®ã¾ã³ãã«è§¦ãã¨ï¼ãã¤ã¤ã³ã°ã¡ãã»ã¼ã¸ãæ®ãã¦æ»ã¬ãã¨ãã§ãã¾ãï¼
retainCount
retainCountãNSLogã§è¡¨ç¤ºãããã¨ã§ï¼ã©ãã§autoreleaseãèµ°ãã調ã¹ãï¼
ã¡ãªã¿ã«ï¼è§£æ¾ããããªãã¸ã§ã¯ãã®retainCountã調ã¹ããã¨ããã¨è½ã¡ãã®ã§æ³¨æï¼
Leaks
Xcodeã®ãã¼ã«ã§Leaksã£ã¦ã®ã使ãã¨ã¡ã¢ãªãªã¼ã¯ã®ãã§ãã¯ãã§ãã¾ãï¼
å®è¡=>ããã©ã¼ãã³ã¹ãã¼ã«ã使ã£ã¦å®è¡=>Leaksã§èµ·åãã¾ãï¼
åããã¦ãã¦Leaks Discoverdã«èµ¤ããã¼ãç«ã£ããã¡ã¢ãªãªã¼ã¯ãã¦ããã¿ããã§ãï¼
ã¨ãããããã®ãã¼ãç«ããªãããã«ããã°ã£ã¦ã¡ã¢ãªãªã¼ã¯ãã¤ã¶ãï¼
ãã®ä»ï¼ãã¬ã¼ã¯ãã¤ã³ããä»æãã¦ï¼å¸¸ã«åç
§ãæãã¦ãããªãã¸ã§ã¯ãã®ä¸èº«ãæ°ã«ããã¨ãï¼è²ã
ãããã©ï¼åºæ¬ã¯ä½ã£ããã®ã¯å¿
ã解æ¾ããã¨ããã¾ã§é¢åãè¦ãï¼
ééã£ã¦ããã¨ãããããããããã¾ãããï¼ä»ã®ã¨ãããããªæãã«é ãæ´çãã¦ãã¾ãï¼
å人çã«ï¼Cã®malloc/freeã§ã¯ãã¾ãã¯ã¾ããªãã£ãã®ã§ï¼ä»åãã®ãããªé ã沸騰ããæ©ä¼ãé ããã¨ãã§ãé常ã«ããããæã£ã¦ãã¾ãï¼
ã©ããã¦ãç解ã§ããªã人ã¯ï¼3æ¥ããããã®æãã®æã使ã£ã¦é ã沸騰ãããã¨ä½ãæããããã«ãªã£ã¦ããï¼
Don't think, feel.ã§ããï¼
ã¡ã¢ãªãããï¼
[追è¨]
Build and Analyze ãæãã¦ãããã¾ããï¼
ããã¯ãããã§ãï¼
記事