Re: Strange NSInvocation behavior when passing pointers
Re: Strange NSInvocation behavior when passing pointers
- Subject: Re: Strange NSInvocation behavior when passing pointers
- From: Philippe Mougin <email@hidden>
- Date: Sun, 14 Jul 2002 22:50:59 +0200
Nathan Day wrote:
Passed pointers are handled like this if you call the -[NSInvocation
retainArguments] method
Ok, good to know. So it should possible to work around the problem for
pointer passed as arguments, simply by making sure the NSInvocation is
configured to not retain its arguments.
But for pointers as return value, it seems that the copying behavior is
always applied :-(
, if you tried to return a pointer to a local variable then that would
not be safe since once the method ends, it's stack frame is no longer
valid. I assume you can't just declare the return type of the method as
(void *), if you can call the method using one of the performSelector:
methods I sure I read somewhere that it will work if the return type is
the same size as an id, so you can just recast your returned value to
the right type. You could perhaps try declaring a method that takes the
same arguments as your method but with a void* return type as a
informal protocol create your NSInvocation using the NSMethodSignature
for this new method but then set the NSInvocation's selector to the
method you really want to call.
Good idea. Sadly, this would not be easy to do in my situation, because
I don't know at compile-time the methods I will need to invoke.
Potentially, it could be any method (actually, I'm adding pointer
support to F-Script).
But along the line of your idea, I can think of a solution: from the
information in the NSMethodSignature of the method that I want to
invoke, I can create a new method signature with a modified return type
(makes it represent a void *). Disadvantage of this solution: it is a
bit convoluted and would require using an undocumented Cocoa method
(i.e. signatureWithObjCTypes:) in order to create the new
NSMethodSignature.
Best,
Phil
On Sunday, July 14, 2002, at 08:53 PM, Philippe Mougin wrote:
When a method returning a pointer (other than an object pointer) is
invoked through a NSInvocation, it seems that you don't get back the
original pointer returned by the method.
Instead, it seems that NSInvocation dereference the pointer, copy the
value it points to, and give you back a pointer to this copy (note: if
the invoked method is declared to return a generic pointer (i.e.
void *), NSInvocation does not have enough information to dereference
the pointer, so in this case it give you back the original pointer)
I haven't verified, but I suppose the same kind of "copying" behavior
is not only applied to return value but also to arguments, in the case
of pointers.
All of this seems similar to the distributed object system behavior
regarding pointers.
Questions:
1) What is the point of doing this in the non-distributed situation?
(Doesn't sounds like a good idea to me).
2) Do you know if this NSInvocation behavior this documented somewhere?
3) In my program, I want to get at the "real" pointers returned or
passed as argument. Is there a way to turn-off this strange
NSInvocation behavior?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.