Q:

addTarget fails for NSInvocation/invoke pair

I'm passing targets and actions to UIButtons from dictionary values.  This works fine EXCEPT if I try and pass an NSInvocation/invoke pair.
//   |self| is a member of |MyClass|, which declares selector |test|

NSDictionary *button1Data = @{ @"selectorString" : NSStringFromSelector(@selector(test)),
                               @"target" : self };

[button1 addTarget:button1Data[@"target"]
            action:NSSelectorFromString(button1Data[@"selectorString"])
  forControlEvents:UIControlEventTouchUpInside];

//  button1 executes target fine

NSInvocation *testInvocation = [NSInvocation invocationWithMethodSignature:[MyClass instanceMethodSignatureForSelector:@selector(test)]];
    testInvocation.selector = @selector(test);
    testInvocation.target = self;
    [testInvocation retainArguments];

NSDictionary *button2Data = @{ @"selectorString" : NSStringFromSelector(@selector(invoke)),
                                       @"target" : testInvocation };

[button2 addTarget:button2Data[@"target"]
            action:NSSelectorFromString(button2Data[@"selectorString"])
  forControlEvents:UIControlEventTouchUpInside];

//  button2 executes gives exceptions

The exceptions from button2 don't have register dumps.
What am I overlooking?

A:

addTarget:action:forControlEvents: does not retain the target. Your NSInvocation object is created locally inside the function and it will be deallocated at the end of the function when nobody has a strong reference to it. Then the button will send a message to a deallocated instance, causing all sorts of bad things.

