Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

implementing copyWithZone:

Quick question about implementing copyWithZone: (NSCopying protocol) in a class. Should the copyWithZone: method be responsible for (auto)releasing the copied object? i.e.
<pre>
-(id)copyWithZone:(NSZone *)zone
{
MyClass *copy=[[[MyClass alloc] init] autorelease];
...
return copy;
}
</pre>
The Apple developer documentation for copyWithZone: doesn't have the method autorelease the copy, but the documentation states elsewhere that the object responsible for alloc/init'ing an object should be responsible for releasing it. Is there a reason that the copyWithZone: method shouldn't autorelease the copy?

Thanks,

Jim

iMac G4, PowerBook G4

Posted on Jan 2, 2006 3:39 PM

Reply
Question marked as Best reply

Posted on Jan 3, 2006 12:53 PM

Should the copyWithZone: method be responsible for (auto)releasing the copied object?


Basically, you should be able generally to treat the copyWithZone: message the same way as a copy: message. That is, the object returned from it should NOT be autoreleased. The object that sends the message will then be responsible for releasing the object it creates in this fashion.

the documentation states elsewhere that the object responsible for alloc/init'ing an object should be responsible for releasing it


The noted exceptions for returning autoreleased objects are the alloc and copy messages. Hence the object that sends the copyWithZone: message becomes responsible.
4 replies
Question marked as Best reply

Jan 3, 2006 12:53 PM in response to Jim McGowan

Should the copyWithZone: method be responsible for (auto)releasing the copied object?


Basically, you should be able generally to treat the copyWithZone: message the same way as a copy: message. That is, the object returned from it should NOT be autoreleased. The object that sends the message will then be responsible for releasing the object it creates in this fashion.

the documentation states elsewhere that the object responsible for alloc/init'ing an object should be responsible for releasing it


The noted exceptions for returning autoreleased objects are the alloc and copy messages. Hence the object that sends the copyWithZone: message becomes responsible.

Jan 5, 2006 10:00 AM in response to Jim McGowan

That's pretty much the idea. Of course, you mostly need to return a copy from a method like your example when you don't want the original object (that you send the message to) modified. Your use of the term "value" is a little confusing in the example just above. I would concoct an example of an instance method for returning a copy of the object to which the message is sent, rather than a copy of one of its instance variables.

Your example seems to be about an object that you want to make a copy of, and then auto-release as expected of an object returned from a method whose name is not copy or alloc. But yes, you should definitely return an autoreleased copy of such an object if you don't want the user of the class to modify the original. Is that clear as mud? I'm almost starting to confuse myself! 😉

implementing copyWithZone:

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.