0

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;

}
Muhammad Nasir
  • 1,796
  • 1
  • 14
  • 22
  • 1
    It's worth noting that in all the hundreds of questions like this that come up, no useful example can ever be done using literal strings. They are compile-time constants and as such don't get treated with standard memory management. – jrturton May 01 '13 at 11:01
  • Possible duplicate of [Objective-C: Why is autorelease (@autoreleasepool) still needed with ARC?](http://stackoverflow.com/questions/9086913/objective-c-why-is-autorelease-autoreleasepool-still-needed-with-arc) – Igor Jan 24 '17 at 13:01

4 Answers4

2

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.

Anil
  • 1,028
  • 9
  • 20
2

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 :)

Community
  • 1
  • 1
FreeNickname
  • 7,398
  • 2
  • 30
  • 60
  • 1
    `string2`'s retain count is not 1 in that example. – bbum May 01 '13 at 13:43
  • I copied this code the question and didn't modify the comment. But why isn't it 1? If we use [[NSString alloc] init] without ARC we get a string with retainCount == 1. Does ARC change this behavior? I'm talking about the first line of code since the comment is in the first line. For now I will remove the comment from the code. Anyway thank you for helping to find the truth :) – FreeNickname May 01 '13 at 16:59
  • 1
    It has nothing to do with ARC. It is entirely an internal implementation detail and indicative of exactly how useless `retainCount` is. `[[NSString alloc] init]` returns a singleton whose retain count is `UINT_MAX`. You can-- and should-- consider the retain count to be `+1` (a delta of +1) and you need to balance that with a `release`, but the absolute retain count's value is useless. – bbum May 01 '13 at 17:02
  • Now I get it. Thank you for your comments! – FreeNickname May 01 '13 at 17:05
0
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 autoreleased 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.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

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.

Suraj Gupta
  • 200
  • 9
  • I know it will be taken care of when the pool is drained. My question is how do i release it immediately. eg i want to release string2 in the same method. so how do i do that – Muhammad Nasir May 01 '13 at 09:38