On 29/04/2009, at 10:48 PM, Тимофей Даньшин wrote:

Why copy mutableRet when you just release it anyway? Instead, just return it with an autorelease:
I do that because i return an immutable array. I always try to return the types i promise to return in the declaration :)



Even so, you're leaking the copy because it should be returned autoreleased, as per correct memory management rules. The fact that you're not doing this here suggests that memory management may be flaky elsewhere, as Kyle suggested.

However, returning the mutable array when your method promises a non- mutable array is perfectly OK. NSMutableArray is an NSArray and therefore the method has fulfilled its contract. If the client code then tests its class and discovers that it is in fact mutable after all, and mutates it, then the client code is the bad guy here. All the copy does is waste time, and serves no purpose. Still, if it makes you feel better...



Note that:

- (NSArray *) selectWordsBeginningWith:(NSString *) s;

Does not include the word new, alloc, copy or mutableCopy and therefore the caller does not own the array returned.




--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to