Re: NSUndoManager retain/release of arguments - ad infinitum
Re: NSUndoManager retain/release of arguments - ad infinitum
- Subject: Re: NSUndoManager retain/release of arguments - ad infinitum
- From: Graham Cox <email@hidden>
- Date: Mon, 10 Jan 2011 17:12:37 +1100
Actually, it doesn't really matter what NSUndoManager does with arguments, all you really need to know is that it follows the rules for ownership of objects, so by and large does the 'right thing'. It does lots of wrong things, IMO, but incorrect memory management isn't among them.
You're right that monitoring retain count isn't going to enlighten you in any useful way, so forget about that.
Kyle is asking the right question: what are you trying to do that means that this matters?
On 09/01/2011, at 7:19 PM, John Bartleson wrote:
> Can someone PUH-LEASE clear up the confusion re NSUndoManager retaining/releasing arguments to prepareWithInvocationTarget:?
>
> In various places in cocoa-dev and in referenced 'net articles, such as here <http://cocoadev.com/index.pl?NSUndoManagerTutorial>
> and here <http://lists.apple.com/archives/cocoa-dev/2001/Jul/msg00682.html> it says that arguments ARE retained.
> In Apple's own documentation ("Undo Architecture") and here <http://cocoadev.com/index.pl?NSUndoManager> it says that
> arguments ARE NOT retained.
>
> Which is it? I get the impression that some time in the last ten years an Apple developer changed this and
> then went off to a tragic death in Cupertino traffic without anyone officially documenting the change.
>
> I've wasted untold days of what could have been productive time trying to straighten this out. How many others have wasted their time?
>
> If a change was made, when was it made (in what version of Mac OS X)? How do we maintain both old and new code to support this properly?
>
> If we check the before/after retain counts of prepareWithInvocationTarget: arguments, it appears that the arguments ARE being retained.
> But wait! According to Apple's guidelines we're not supposed to use retainCount for this purpose.
>
> And we lose an innocent bystander in the confusion: the supposedly hard and fast rule that if you create an object with alloc, new,
> copy, or mutableCopy you are responsible for releasing it, otherwise you're not responsible for releasing it.
>
> Apparently, the rule should be supplemented with "But if you create an object with alloc, new, copy, or mutableCopy, then pass it
> as an argument to prepareWithInvocationTarget: you're not responsible for releasing it."
No, this is wrong. -prepareWithInvocationTarget: only takes one argument - the target, and targets are NOT retained (targets, delegates, data sources etc are not retained, according to other well-documented rules). Maybe you're getting confused by how -prepareWithInvocationTarget: is typically used, with a follow-on message to the object it returns on which you invoke some method which really exists within the target class. The object returned by the undo manager turns that into an NSInvocation internally, which almost certainly does retain its arguments.
There are no exceptions to the MM rules here. If NSUndoManager needs to retain or copy the object(s) passed as arguments, it will do so according to its own logic, but if you own them, you are still responsible for releasing them when you're done with them.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden