> On Apr 21, 2015, at 12:08 PM, Jens Alfke <[email protected]> wrote:
> 
> Under ARC, what’s the recommended/supported way to message an object, given 
> the object reference and a selector? I’m implementing an instance of the 
> target-action pattern that’s all over Cocoa, but running into roadblocks:
> 
> * If I try to use -[NSObject performSelector:withObject:] I get a compiler 
> error “PerformSelector may cause a leak because its selector is unknown”.
> 
> I’m aware that _in general_, dynamic dispatch can cause bad side effects 
> under ARC. But all I want to do is send a message that takes an NSObject 
> parameter and returns void, which is safe.

It is in fact *not* safe to use -performSelector: to do that. ARC may attempt 
to perform the caller's side of the return-autoreleased optimization on the 
result of -performSelector:. That goes poorly if the return value is 
uninitialized.


> * If I try to call objc_msgSend directly, it doesn’t work because 
> <objc/message.h> has purposely obfuscated the real signature of the function 
> to hide its parameters. (Unless I redefine OBJC_OLD_DISPATCH_PROTOTYPES as 1, 
> but even if I do that it doesn’t take effect; I’m suspecting message.h gets 
> imported during my prefix header or something.)

You can cast objc_msgSend to the appropriate function pointer type and call 
that. The new declaration in objc/message.h is intended to force you to add the 
cast.

Adapting your example:

// [self.delegate performSelector: action withObject: newWidget];
void (*msgSend_action)(id, SEL, Widget*) = (typeof(msgSend_action))objc_msgSend;
msgSend_action(self.delegate, action, newWidget);


-- 
Greg Parker     [email protected]     Runtime Wrangler



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to