In a book, the following is said:
So how do you know when an object is owned, and by whom? Consider the
following example:NSString *str = [[NSString alloc] initWithString:@”Hello”]; NSString *str2 = str;In this example, you use the
allockeyword forstr, so you ownstr.
Therefore, you need to release it when it’s no longer needed. However,
str2is simply pointing tostr, so you do not ownstr2, meaning you
need not releasestr2when you are done using it.
I thought ownership is by object, not variable or pointer… so we can’t say we “own str” or “own str2“… we own an object, which is pointed to by either str or str2, and if we use [str release] or [str2 release], it is all the same.
The other description is:
For example, consider the example used in the previous section:
NSString *str = [[NSString alloc] initWithString:@”Hello”]; NSString *str2 = str; [str release]; [str2 release]; //---this is not OK as you do not own str2---Attempting to release
str2will result in a runtime error because you
cannot release an object not owned by you.
We can actually use [str2 release] if that is called before [str release]. If we do that, then the line [str release] will cause an error because now str as well as str2 are both dangling pointers, and supposedly when release was sent to the object the first time, the reference count became 0, and dealloc was called immediately, and the memory was freed by the C function free().
Is the above correct, or maybe there is something else to be corrected?
Don’t think of it in terms of managing memory, but in terms of object ownership. You obtain ownership of an object when you allocate it, retain it, or copy it. You are responsible for releasing exactly the objects you own, not others.
In your example the assignment to
str2does not take ownership of the object, but if you really need a second “owning” reference to it, then you ought to do[str2 retain], after which it is not an error to do[str release]; [str2 release];. This is also what would happen automatically using ARC, unless you annotatedstr2as a weak reference. (Of course in this simple case the unnecessary retain/release could be optimized away internally by the compiler.)