0

I was reading document about ARC and came across a part which confused me.Please can anyone help me. I have two questions: 1.I know ARC release object when no variable is pointing to it.Now suppose I have some method say:(NSString *)returnString, whose return type is NSSTring;So what will happen to the variable which is declare in the method returnString?I know the receiving variable will get release when it comes out of scope of the method where returnString is called but what will happen to the variable which is inside returnString.

-(NSSTring *)returnString
{
   NSString *str = //Some value;

   return str;//What will happen to this.
}

and Other method:

-(void)useString
{
   NSString *str1 = [self returnString];  //It will get release when this method gets over.
}

2.What will happen if returnString is in some third party library which isn’t compiled with ARC?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36

2 Answers2

2

ARC just inserts the [... retain] and [... release] methods calls at compile time. In your case, it sees that 'str' is a local variable and adds an autorelease method call just after your declaration. So really, the memory gets deallocated in the same way, but you don't have to worry about it, ARC does it for you.

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
  • The compiler does go through and optimise the code once the `-retain`, `-release` and `-autorelease` have been added, thus a lot of the code added can be removed again, even across calls. A lot fewer object will end up in the autorelease pool. – hypercrypt Jun 18 '12 at 15:42
0

For your first question, ARC will take care of retaining and releasing for you. Any time you're still maintaining a reference to a variable, ARC should be holding on to it. In your specific example the string will likely be returned as an autoreleased object, but the actual implementation is hidden from you.

For your second question, you can mark libraries as not using ARC in order to keep manual retain/release. To do so, see this question: ios5 ARC what is the compiler flag to exclude a file from ARC?

Community
  • 1
  • 1
kevboh
  • 5,207
  • 5
  • 38
  • 54
  • Thanks for your reply.I got first answer.But for second question you mean to say, I need to autorelease str manually in second case? – Nuzhat Zari Jun 19 '12 at 05:30
  • If a file is marked "-fno-objc-arc", then you still need manual retain/release/autorelease. – kevboh Jun 19 '12 at 11:43