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
alloc
keyword forstr
, so you ownstr
. Therefore, you need to release it when it’s no longer needed. However,str2
is simply pointing tostr
, so you do not ownstr2
, meaning you need not releasestr2
when 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
str2
will 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?