Reputation: 65
Consider a situation implemented in Manual Retain Release, where I have a variable pointing to an object returned by a method.
{
...
NSString *str = [self myNewString];
...
}
- (NSString *)myNewString
{
NSString *myString = [NSString stringWithFormat:@"%d-String", 1];
return myString;
}
Here do we have to retain the object returned by myNewString
so that it wont be released while we are using it?
Please help I am new to Objective-C. Thanks in advance.
Upvotes: 2
Views: 1278
Reputation: 437552
There are two aspects to this answer:
First, you want to conform to the method naming rule, that dictates that any method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” should have a +1 retain count (i.e. ownership is transferred), otherwise it should be an autorelease
object (or, more accurately, any object whose ownership is not transferred, and if the caller wants to claim ownership, it would have to manually retain
it).
By the way, ARC uses these method name prefixes to determine the object memory semantics automatically, but in manual retain and release (MRR) code, the burden rests with the programmer to ensure that the memory management of the method conforms to the semantics implied by the method's name. Following this method naming rule will become important if you ever integrate this MRR code with ARC code at some future date. There are code hints you can use if you have legacy code that violates this method naming convention, but it's really just better to make sure your code's memory semantics are in conformance with this method naming rule.
Regardless, this method naming rule is included in the Basic Memory Management Rules outlined in the Advanced Memory Management Programming Guide.
So, in manual retain and release, you have one of two choices. If the method starts with “alloc”, “new”, “copy”, or “mutableCopy”, then it should transfer ownership by returning a +1 retainCount
object, e.g.:
- (NSString *)newSomeString
{
return [[NSString alloc] initWithFormat:@"%d-String", 1];
}
otherwise, it should not transfer ownership (e.g. return an autorelease
object), such as:
- (NSString *)someString
{
return [NSString stringWithFormat:@"%d-String", 1];
}
So, if the caller wanted to make sure the object was retained, it could either just call the method that returned the +1 object:
NSString *string = [self newSomeString];
or, call the version that returns an autorelease
object, but then explicitly retain
it:
NSString *string = [[self someString] retain];
In practice, the latter convention, the someString
example, is more common (use method that returns autorelease
object, i.e. where the method name does not start with “alloc”, “new”, “copy”, or “mutableCopy”; and then if the caller needs to retain
it, it should just explicitly do so). The new
, copy
, and mutableCopy
methods all are generally used within respective, very specific contexts, which don't apply here.
By the way, if you run your code through the static analyzer ("Analyze" on Xcode's "Product" menu), it does a remarkably good job warning you if your MRR code has issues with over retaining objects or failing to retain them.
But bottom line, carefully follow the basic memory management rules, including the convention for naming methods, and then if you need it retained, explicitly retain
your autorelease
object or call a method that returns a +1 retainCount
object.
Upvotes: 4
Reputation: 56059
Basically any method that returns a new object besides new
/alloc
+init
Returns it without ownership, meaning that you can count on it sticking around until the enclosing autoreleasepool drains, but no telling after that. If you want to keep it around longer, you must retain it.
Upvotes: 0
Reputation: 3607
Here you are passing just the address of location where NSString object is stored. So, definitely object would be retained by compiler. But you don't have to worry. ARC will take care of releasing memory once the reference count is 0. Here you go Learn more about ARC
Upvotes: 0