Autorelease pool
Upcoming SlideShare
Loading in...5
×
 

Autorelease pool

on

  • 726 views

 

Statistics

Views

Total Views
726
Views on SlideShare
726
Embed Views
0

Actions

Likes
1
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Autorelease pool Autorelease pool Presentation Transcript

  • Autorelease PoolMichael Pan13年5月27⽇日星期⼀一
  • 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];return name;}Memory leak!!!iOS 413年5月27⽇日星期⼀一
  • 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];[name release]; // ??return name;}iOS 413年5月27⽇日星期⼀一
  • 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];return name;[name release]; // ??}iOS 413年5月27⽇日星期⼀一
  • 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];[name autorelease];return name;}NSString * outer = [customer name];[outer retain];//....[outer release];iOS 413年5月27⽇日星期⼀一
  • 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];// [name autorelease];return name;}ARC13年5月27⽇日星期⼀一
  • Name does matter• begin with alloc/new/copy/mutableCopy will not be putinto autorelease pool• others will add object into autorelease pool automatically-(id) newCar;13年5月27⽇日星期⼀一
  • Demo• ARCDemo13年5月27⽇日星期⼀一
  • Pool操作物件時機• 加⼊入• 使⽤用[obj autorelease]• 常數字串• class level constructor• 呼叫[obj release]• 當[pool drain]• event loop 結束autorelease pool 無法 retainNO Arc13年5月27⽇日星期⼀一
  • ⾃自⾏行產⽣生Pool• NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];• 新增多個pool會以stack ⽅方式儲存• 物件呼叫 autorelease時會被加⼊入其最近的pool• 某個pool 呼叫 drain 之後,其上的pool 也都會被drainpool1pool2int main(){NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];}NO Arc13年5月27⽇日星期⼀一
  • RecapNSMutableString * s1 = [[NSMutableString alloc] initWithString:@"hello"];NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];!NSArray * staticArray = [NSArray arrayWithObject:s1];NSLog(@"s1 retain count is %lu", [s1 retainCount]);[pool1 drain];NSLog(@"s1 retain count is %lu", [s1 retainCount]);Result is ?pool2pool1staticArrays1iOS 413年5月27⽇日星期⼀一
  • ⾃自⾏行產⽣生Poolint main(){@autoreleasepool{//...}}iOS 513年5月27⽇日星期⼀一
  • Clean pool nicelyint i;for (i = 0; i < 1000000; i++) {id object = [someArray objectAtIndex: i];NSString *desc = [object description];// and do something with the description}NSAutoreleasePool *pool;pool = [[NSAutoreleasePool alloc] init];int i;for (i = 0; i < 1000000; i++) {id object = [someArray objectAtIndex: i];NSString *desc = [object description];// and do something with the descriptionif (i % 1000 == 0) {[pool drain];pool = [[NSAutoreleasePool alloc] init];}}[pool release]iOS 413年5月27⽇日星期⼀一
  • Clean pool nicelyint i;for (i = 0; i < 1000000; i++) {id object = [someArray objectAtIndex: i];NSString *desc = [object description];// and do something with the description}int i;for (i = 0; i < 1000000; i++) {@autoreleasepool{id object = [someArray objectAtIndex: i];NSString *desc = [object description];// and do something with the description}}iOS 513年5月27⽇日星期⼀一
  • EqualNSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];[obj autorelease];[pool drain];@autoreleasepool{id __autoreleasing obj2;obj2 = obj;}ARCno ARC13年5月27⽇日星期⼀一