Re: NSInvocation's getArgument & setReurnValue question
Re: NSInvocation's getArgument & setReurnValue question
- Subject: Re: NSInvocation's getArgument & setReurnValue question
- From: Ken Thomases <email@hidden>
- Date: Sat, 20 Apr 2013 09:52:18 -0500
On Apr 18, 2013, at 2:27 AM, Christ Levesque wrote:
> I used - getArgument:atIndex: method but it gives me error. I don't know what's the problem. I used this as below:
>
> if ([component isEqualToString: @"class"]) {
> 1) id arg;
> 2) [invocation getArgument: &arg atIndex: i + 2];
> 3) [self class:arg];
> }
>
> The error is this:
> NSInvocation's getArgument is not safe to be used with an object with ownership other than __unsafe_unretained.
> The error is colored Red.
Declare "arg" as "__unsafe_unretained id arg;". If "invocation" was declared locally, rather than being passed in as a parameter, then it would probably be best to add "[invocation self]" after the last reference to "arg". That would be necessary to prevent ARC from releasing invocation and, implicitly, arg before you're done with it.
> And also I used -setReturnValue: and it gives me two errors.
>
> [invocation setReturnValue: &self];
>
> The first is:
> NSInvocation's setReturnValue is not safe to be used with an object with ownership other than __unsafe_unretained.
> The second is:
> Sending 'DocHTMLElement *const __strong *' to parameter of type 'void *' changes retain/release properties of
> pointer.
> The error is colored Red.
Change that line to:
__unsafe_unretained id unsafeSelf = self;
[invocation setReturnValue:&unsafeSelf];
The common thread among all of these errors is that, with ARC, the compiler needs to reason about the memory management implied by various method calls but these methods of NSInvocation are opaque to it. It can't determine, just from the method signatures, whether it needs to retain or release the objects whose pointers are passed by reference.
You don't say what you're using NSInvocation for. There's a very good chance that there are better techniques available that don't have these sorts of complications with ARC. In particular, you should see if blocks can be used instead.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden