-(IBAction) testTemp: (id) sender{
id tempObj;
tempObj = otherObject; //the otherObject will be released in dealloc met开发者_开发技巧hod;
[tempObj doSomething];
}
As you can see, I use the tempObj for temp use. I won't use it after the user quit this method, should I need to release the tempObj? and why?
No you do not need to release the tempObj. The reason is because you do not retain it or do any memory management on it. All that tempObj is a pointer to the otherObject and you are just using it as an alias.
In the code is there any reason not to use otherObject directly - if in the real code you do need to use tempObj then there might well be some memory management involved.
tempObj is not an object. tempObj is a variable. tempObj contains a pointer to the object and the assignment tempObj = otherObject makes otherObject contain the same pointer.
The object is a portion of memory. Each object has a counter, the reference counter. [tempObj retain] adds 1 to the reference counter, [tempObj release] subtracts 1 from the reference counter. It's a way to know in how many places you're using your object. When the count reaches 0 the object is declared dead. No one should be using it anymore: the memory portion where it lived will likely be used by some other object.
So you will not want to release otherObject or tempObj, which will obtain the same effect (subtracting 1 from the reference count of the object). If you do that, the reference count could reach 0 and the release in the dealloc method it will likely cause a crash.
精彩评论