The problem is that by default result is __strong , so when it goes out of scope, the compiler creates a release for it. But getReturnValue: did not give you ownership of the returned object, so your method should not issue it.
You can fix this by modifying the result declaration:
__unsafe_unretained id result;
This prevents the compiler from creating a release for result when the result out of scope. If you need to save it, you can copy it to another __strong variable.
You can also add a category to NSInvocation to handle this for you:
@interface NSInvocation (ObjectReturnValue) - (id)objectReturnValue; @end @implementation NSInvocation (ObjectReturnValue) - (id)objectReturnValue { __unsafe_unretained id result; [self getReturnValue:&result]; return result; } @end ... if (length == 8) { id result = [invocation objectReturnValue]; } ...
You may also report this as an error. I was expecting the compiler, or at least the static analyzer, to warn you that you are converting a pointer to a strong id to a void pointer. http://bugreport.apple.com
source share