3

NSMenuItem -setTarget: Does it retain the target, or should one explicitly retain it?

I've seen conflicting docs on this. I know of retainArguments in NSInvocation, but I'm not sure this applies to NSMenuItem as it doesn't inherit from NSInvocation.

the979kid
  • 635
  • 5
  • 15

2 Answers2

3

I don't believe it does. Usually target-action methods and delegate properties don't retain what they're set to, as they don't “own” their target.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • It's a good idea to file an enhancement request by using the link at the bottom of the relevant page. A quick look at the -setTarget: API reference confirmed this is not stated either way, though it should be. – Joshua Nozzi Jul 27 '10 at 21:37
1

Simply look at the header:

@property (nullable, weak) id target;

or in Swift:

weak var target: AnyObject? { get set }

Here we see that target is weak, meaning that it doesn't have control over the target's lifetime. In technical terms, it neither increments nor decrements the reference count. Once your target is deallocated for any reason, NSMenuItem loses it too.

Ky -
  • 30,724
  • 51
  • 192
  • 308