Using Objective-C method_invoke to call void method in ARC

On iOS, I'm trying to use a function method_invoke

from the Objective-C ( reference ) runtime to call an Objective-C method that is declared with a return type void

.

This works fine in non-ARC code, but with ARC enabled, I get a crash after calling a method in objc_retain

. I think what is happening is that the compiler notices the method_invoke

return type id

and tries to store the return value method_invoke

(note that it is method_invoke

intended to return the return value of the method it calls).

What's the correct way to let the compiler know that in this particular case, the return value method_invoke

is garbage and shouldn't be saved? The following works, but seems conceptually wrong:

(void)((__bridge void *)method_invoke(target, method));

It doesn't work (still crashing in objc_retain

:

(void)method_invoke(target, method)

Is there a better approach here?

+2


source to share


1 answer


This question gave me an idea for a better solution.

The basic approach was to create a function pointer referencing method_invoke

with the correct signature (void return type) and cast method_invoke

on that function pointer, and then call the function pointer.



So roughly:

static void (*_method_invoke_void)(id, Method, ...) = (void (*)(id, Method, ...)) method_invoke;
... snip ...
_method_invoke_void(target, method);

      

+3


source







All Articles