What automatically retains and releases?
I kind of understood what retains and releases automatically but I can't find any official data on this and I think I got something wrong.
- Setting properties that are declared with @property(retain) will retain and release the old value, but not setting the properties inside the same object without
self.
- All objects inside a method(including parameters) are retained when created/passed and released when the method returns.
- An object that is returned by a method will not be released but instead will be released by the method its returned to since its local variable inside the method.
Did I get something wrong/forgot anything?
What will happen when the return is discarded? like[object someMethodThatReturnsAnObject];
. it will not be local to the开发者_StackOverflow社区 method its returned to so I'm not sure if it will be released and xCode warns about it.Instead of guessing, read the documentation. It is definitive.
Setting properties that are declared with @property(retain) will retain and release the old value, but not setting the properties inside the same object without self.
Sort of. There is no "setting a property without self". That is setting an instance variable directly.
All objects inside a method(including parameters) are retained when created/passed and released when the method returns.
Nope; see the docs.
An object that is returned by a method will not be released but instead will be released by the method its returned to since its local variable inside the method.
Not at all; see the docs.
Setting properties that are declared with @property(retain) will retain and release the old value, but not setting the properties inside the same object without self.
Calling a setter method that was synthesized for a retain property will release the old value and retain the new one. Setting an ivar directly, without going through your accessor, will do nothing but assign the ivar. It will neither retain nor release anything. self.foo = bar
is exactly [self setFoo:bar]
. Whatever setFoo:
does will be done (a synthesized retain version of setFoo:
works as described above). foo = bar
is exactly foo = bar
.
All objects inside a method(including parameters) are retained when created/passed and released when the method returns.
This is not true at all. They are neither retained nor released. Retaining and releasing does not happen magically. It happens in response to calls to alloc
, new
, copy
, retain
on the one hand and release
on the other. Using dot notation is just a short-hand for a method call which may have a retain
inside of it.
An object that is returned by a method will not be released but instead will be released by the method its returned to since its local variable inside the method.
This is not true. An object returned by a method will be neither retained nor released. By convention, a method with alloc
, new
or copy
in its name will return a net +1 retain. Any other method will return a net 0 retain-count (there will be as many autoreleases on the object as retains). The "current" retain count will always be greater than 0 or else the object could not be returned. (This is a slight gloss on the truth. The retain count in either case could be greater than 1 if there are private retains. But from the point of view of the caller, this is a useful way to think about it.)
The best place to look is Practical Memory Management, which lays this all out very concisely. The rest of the Memory Management Programming Guide will give more examples.
The term "Automatically" is a bit scary. It implies that the retain and release are somehow magically applied by the runtime behind the scenes. Really truly, the only time something is retained is when it is sent a message that contain one of the following: New Alloc Retain Copy (N.A.R.C). The only time it is released is when it is sent either an release message or an autorelease message. So what you really need to understand is, in what situations are these methods sent?
1. Setting Properties
You're correct, in as far as you went. Why? Because properties are just syntactic sugar for generating getter and setter methods. For example, self.myString = @"Foo";
is absolutely identical to [self setMyString:@"Foo"]
. What you need to understand is that, when you declare a property with retain semantics, you're actually given a method that looks like this:
- (void)setMyString:(NSString *)newString {
if ( newString != myString ) {
[myString release];
myString = [newString retain];
}
}
Thus, obviously, saying self.myString = someOtherString
will cause the new value to be retained. What you refer to as "Setting a property without self.
" is actually just direct ivar assignment. Since no dot operator is used, and therefor no method invoked, you know that nothing is retained.
2. and 3. Something To Do With Method Scope?
Neither of these are true at all. Convention says that variables returned my methods without traces of NARC in the name will be autoreleased. It's up the method author to actually follow the convention. Nothing is ever automatically retained or released just as a result of method invocation.
精彩评论