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
- alloc,
- copy,
- mutableCopy
- 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!