1
votes

I was reading article related to ARC.Following is the section:

ARC also taps into a the Objective-C language naming conventions and infers the ownership of the returned object. In Objective-C, a method that stats with any one of the following prefix

  1. alloc,
  2. copy,
  3. mutableCopy
  4. and new

are considered to be transferring ownership of the returned object to the caller. This means, in your application, when you create a method, ARC automatically infers whether to return a autoreleased object or a +1 retained object from your method name. However, there is a >small caveat. Let’s assume that you have a method that starts with “copy”, as in

-(NSString*) copyRightString;

ARC assumes that it would transfer the ownership of the returned string to the caller and inserts a release automatically. Everything works well, if both the called method and the calling method are compiled using ARC.

But if your “copyRightString” method is in a third party library that isn’t compiled with ARC, you will over-release the returned string. This is because, on the calling code, ARC compiler inserts a release to balance out the retain count bumped up by the “copy” method.

Conversely, if the third party library is compiled with ARC and your method isn’t, you will have a memory leak.

Now I have two question: 1.Why object is over-released when “copyRightString” method is in a third party library that isn’t compiled with ARC?Since method name starts with copy, so the method will not release the object because it is the responsibility of calling method to release the object which ARC will take care since the name of method begins with copy.

2.Why there will be memory leak if the third party library is compiled with ARC and our method isn’t?Since method name begins with copy so ARC will not release it and it is responsibility of calling method to release it.and in our code we will release it since then method name begins with copy.

I hope I am clear!

1
yes sure will work on that.But can you please answer my question?Nuzhat Zari
nobody will until you drastically improve it :)Martin
If I get required answer then I definitely accept it.and now if I get required answer I will accept it.Nuzhat Zari
Hope now someone will answer.Nuzhat Zari

1 Answers

3
votes

In this example, the method name -copyRightString was chosen to show a certain problem. The method signature suggests that the method returns a string containing copyright information, so programmers would expect that it returns an autoreleased value. But accidentally the method name starts with copy, so ARC expects it to return a retained value. A method implementation could look like this:

- (NSString *)copyRightString
{
   return [NSString stringWithFormat:@"Copyright %d %@", 
                                     self.copyRightYear,
                                     self.companyName];
}

If you compile this method in a third party library without ARC, the method will return an autoreleased value. If you now use it from ARC code, ARC sees that the name starts with copy, so it expects this method to return a retained (not autoreleased) value. Because of that, it will release the return value one time too often. That's the over-release part.

On the other hand, if you compile this method with ARC, ARC sees that the name starts with copy, so it will make sure that the method returns a retained value. Programmers using this method from non-ARC however may expect the method to return an autoreleased value (as the method signature suggests that it returns a string containing copyright information). So they would not release the object in their code, resulting in a memory leak.