Order of retain and release at page 68-69


#1

At page 68 bottom, there is
[str retain];
[possessionName release];
possessionName = str;

At page 69 top, it says the order should be retain and release.
Can someone please explain why the order can not be reversed?
I do not understand the difference it makes when reversed.
Thank you.


#2

As pointed out in the text, the difference comes into effect when str and possessionName refer to the same object. If in that case you would first release possessionName its retain count would drop to zero, causing it to become deallocated. Next, you try to retain str, but str no longer exists …

The text states: “More often than you might imagine, possessionName and str will point at the same object”.

Hope this helps.


#3

Thank you very much.

I do not know why I did not understand it from the book.
Probably, I thought that the str and possessionName are always different, otherwise there would be no point of setting the possesionName.

Thank you for your explanation.