If I have a custom method like this:
- (void)myMethod:(id)myArgument
{
//do something with myArgument
}
is myArgument guaranteed to stick around for the execution of that method if i don’t take ownership of it?
EDIT
Let me elaborate further. Say i call this somewhere:
[self myMethod:_myIvar];
and then somewhere else, while myMethod is executing, someone calls this:
[_myIvar release];
does that effect the argument within myMethod?
END EDIT
Looking through documentation/sample code, i rarely see a [myArgument retain] or [myArgument copy] at the top of a custom method. So is it unnecessary?
Thanks!
If
myArgumentis a valid object at the start ofmyMethod, and you do nothing to cause it to be deallocated or otherwise collected during the method, then yes,myArgumentwill still be a valid object at the end of the method.EDIT in response to question’s edit: let’s talk about object ownership in this case. When you’re passed
myArgumentas an argument to your function, you – in the context ofmyMethod– don’t declare an interest in the object byretaining orcopying it. In such a situation, the only owners of the object exist outsidemyMethod.If the last owner of
myArgumentdecides that it’s done withmyArgumentandreleases it, then it’s entirely possible thatmyArgumentgoes away – there are no strong references to the object any more that would keep it around. It may be valid, depending on threading concerns, autorelease pools involved, and a whole host of other issues, but that’s a dangerous game to play. If there’s even a possibility that such a situation might happen, you should explicitly declare an interest inmyArgumentwithinmyMethod, as recommended by the Memory Management Policy.