Autorelease pool
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
797
On Slideshare
797
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
4
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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