3

I am new to XPC service programming, and I am trying to share a IOSurface created in a XPC service to the host application. I can setup a simple host and service using NSXPCConnection API, and now I think I am supposed to use these two methods (found in IOSurfaceAPI.h) to pass the IOSurface to the host:

IOSurfaceRef IOSurfaceLookupFromXPCObject(xpc_object_t xobj);
xpc_object_t IOSurfaceCreateXPCObject(IOSurfaceRef aSurface);

Is there a way I can pass the xpc_object_t using the NSXPCConnection API, or should I work with the C API (as defined in xpc.h)?

Andrea Cremaschi
  • 533
  • 3
  • 19

1 Answers1

2

Good question. The "Daemons and Services Programming Guide" states that XPC-proxied methods can handle only the following data types:

  • Arithmetic types (int, char, float, double, uint64_t, NSUInteger, and so on)
  • BOOL
  • C strings
  • C structures and arrays containing only the types listed above
  • Objective-C objects that implement the NSSecureCoding protocol

xpc_object_t is just a typedef for void *, so it doesn't meet any of those criteria, and I don't see any way to wrap it in something that does. (Trying to convert it to data, or cast it to an appropriately-sized int, won't work.)

On the other hand, on 10.8, xpc_object_t also is enough of an Objective-C object to be retained/released with ARC, to be added to Objective-C collections, etc.

It would be easy enough to try passing an xpc_object_t over your protocol, just to see if Apple left an undocumented loophole. Try it!

Otherwise, the lower-level XPC API will definitely work:

 xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
 xpc_object_t xpcSurface = IOSurfaceCreateXPCObject(aSurface);
 xpc_dictionary_set_value(message, "surface", xpcSurface);
 xpc_connection_send_message(connection, message);
Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
  • Thank you Kurt, I tried to pass an xpc_object_t as an argument of my protocol's method, but this just doesn't work (SIGABRT at runtime, no compiler errors). No loopholes unfortunately!! So I tried the other way, but as it seems it is not straightforward to mix NSXPCConnection wrapper code with C API: as far as I can understand, the wrapper hides its xpc_connection_t reference in a private ivar ( void *_xconnection ). But this is another topic. So, to answer my own question, it seems that IOSurfaceRef isn't simply compatible with the Foundation's Objective-C wrapper.. – Andrea Cremaschi Jan 01 '13 at 13:10
  • That's what I expected. I highly recommend filing a bug with Apple -- if people complain about a bug, it makes it more likely that they will fix it. – Kurt Revis Jan 01 '13 at 20:23
  • Bug filed with ID 12942765. Thanks Kurt! – Andrea Cremaschi Jan 02 '13 at 09:06
  • Any news from the bug report 12942765 ? – vtruant May 13 '19 at 08:30