简体   繁体   中英

what does @autoreleasepool do?

I am using ARC and i know the autoreleasepool sends release to all elements inside it. Consider the following example.

-(NSString*)someMethod{

    NSString *string1=@"sample text inside string 1"; // string1 add to main autorelease pool
    NSString *string2=[[NSString alloc] init];        // string2 retain count is 1

    string2=@"sample text inside string 2";           // just assigning

    return string1;

}

-(void)viewDidLoad{

    NSString *returnedString=[self someMethod];

}

1. so my question is how do i completely release string2 in someMethod?

2. How to completely release string1 from viewDidLoad method completely?

Note: I am using ARC

if i use @autoreleasepool in someMethod then i think it will destroy both string1 and string2. wont it?

And what will be the effect of using @autoreleasepool in someMethod

-(NSString*)someMethod{

    @autoreleasepool{

    NSString *string1=@"sample text inside string 1"; // string1 add to main autorelease pool
    NSString *string2=[[NSString alloc] init];        // string2 retain count is 1

    string2=@"sample text inside string 2";           // just assigning

    }
    return string1;

}

You can't release any object, instance if you are using ARC. It is handled by complier itself when you use ARC.

So, even string will be released the same way.

First of all,

NSString *string2=[[NSString alloc] init];
string2=@"sample text inside string 2";  

string2 is not an object itself. It is a pointer to an object. So here you create an object, get a pointer to it, and then immediately replace it with a pointer to another string. If you didn't use ARC you would leak memory here. Take a look at @Anoop Vaidya's answer for more details about it.

As for your questions: when you use ARC, the compiler automatically tracks objects and releases them when they are no longer used. So you don't have to release it manually. You could assign a pointer to nil, but as soon as I remember it is not necessary.

The code from your second example won't compile, because string1 is declared inside the autorelease scope. And actually this @autorelease here doesn't make any difference since you don't create autoreleased objects inside it.

This question contains more details about using autorelease pool with ARC. And Apple's official documentation is probably the best I've ever seen, so please feel free to have a look at their Advanced memory management programming guide . It isn't too long, and it explains everything in details :)

NSString *string2=[[NSString alloc] init];        // string2 retain count is 1
string2=@"sample text inside string 2"; 

Bad way to doing.

Instead do this way

NSString *string2=[[NSString alloc] initWithString:@"sample text inside string 2"];

Or even

NSString *string2=@"sample text inside string 2";

string1 is in autorelease d mode, you dont have to release it.

As you are using ARC(Automatic Reference Counting) . You never release object. The compiler takes care of it.

You don't need to release it ,it will automatically done by ARC. it will give an error if you try to release any object while using ARC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM