Autorelease PoolMichael Pan13年5月27⽇日星期⼀一
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];return name;}Memory leak!!!iOS 413年5月...
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];[name release]; // ??return name;}iOS...
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];return name;[name release]; // ??}iOS...
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];[name autorelease];return name;}NSStr...
回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];// [name autorelease];return name;}AR...
Name does matter• begin with alloc/new/copy/mutableCopy will not be putinto autorelease pool• others will add object into ...
Demo• ARCDemo13年5月27⽇日星期⼀一
Pool操作物件時機• 加⼊入• 使⽤用[obj autorelease]• 常數字串• class level constructor• 呼叫[obj release]• 當[pool drain]• event loop 結束autorel...
⾃自⾏行產⽣生Pool• NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];• 新增多個pool會以stack ⽅方式儲存• 物件呼叫 autorelease時會被加⼊入其最...
RecapNSMutableString * s1 = [[NSMutableString alloc] initWithString:@"hello"];NSAutoreleasePool *pool1 = [[NSAutoreleasePo...
⾃自⾏行產⽣生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 de...
Clean pool nicelyint i;for (i = 0; i < 1000000; i++) {id object = [someArray objectAtIndex: i];NSString *desc = [object de...
EqualNSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];[obj autorelease];[pool drain];@autoreleasepool{id __autore...
Upcoming SlideShare
Loading in...5
×

Autorelease pool

421

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
421
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Autorelease pool

  1. 1. Autorelease PoolMichael Pan13年5月27⽇日星期⼀一
  2. 2. 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];return name;}Memory leak!!!iOS 413年5月27⽇日星期⼀一
  3. 3. 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];[name release]; // ??return name;}iOS 413年5月27⽇日星期⼀一
  4. 4. 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];return name;[name release]; // ??}iOS 413年5月27⽇日星期⼀一
  5. 5. 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];[name autorelease];return name;}NSString * outer = [customer name];[outer retain];//....[outer release];iOS 413年5月27⽇日星期⼀一
  6. 6. 回傳新產⽣生的物件-(NSString *) name{NSString * name= [[NSString alloc]initWithString:@”Bob”];// [name autorelease];return name;}ARC13年5月27⽇日星期⼀一
  7. 7. 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⽇日星期⼀一
  8. 8. Demo• ARCDemo13年5月27⽇日星期⼀一
  9. 9. Pool操作物件時機• 加⼊入• 使⽤用[obj autorelease]• 常數字串• class level constructor• 呼叫[obj release]• 當[pool drain]• event loop 結束autorelease pool 無法 retainNO Arc13年5月27⽇日星期⼀一
  10. 10. ⾃自⾏行產⽣生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⽇日星期⼀一
  11. 11. 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⽇日星期⼀一
  12. 12. ⾃自⾏行產⽣生Poolint main(){@autoreleasepool{//...}}iOS 513年5月27⽇日星期⼀一
  13. 13. 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⽇日星期⼀一
  14. 14. 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⽇日星期⼀一
  15. 15. EqualNSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];[obj autorelease];[pool drain];@autoreleasepool{id __autoreleasing obj2;obj2 = obj;}ARCno ARC13年5月27⽇日星期⼀一
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×