2

I'm trying to use an XPC Service for inter-process communication. I've added an XPC service target to my project, and then when I try doing a xpc_connection_send_message I get EXC_BAD_INSTRUCTION. As far as I can tell, I am correctly initializing and starting the connection.

serviceConnection = xpc_connection_create("com.foo.bar.MyService", dispatch_get_main_queue());

xpc_connection_set_event_handler(serviceConnection, ^(xpc_object_t event) {
    xpc_type_t type = xpc_get_type(event);

    if (type == XPC_TYPE_ERROR) {
        if (event == XPC_ERROR_CONNECTION_INTERRUPTED) {
            // The service has either cancaled itself, crashed, or been
            // terminated.  The XPC connection is still valid and sending a
            // message to it will re-launch the service.  If the service is
            // state-full, this is the time to initialize the new service.
        } 

        else if (event == XPC_ERROR_CONNECTION_INVALID) {            
            // The service is invalid. Either the service name supplied to
            // xpc_connection_create() is incorrect or we (this process) have
            // canceled the service; we can do any cleanup of appliation
            // state at this point.
            NSLog(@"Uh oh, connection invalid");
            xpc_release(serviceConnection);
            serviceConnection = nil;
            //xpc_release(stack);
            //stack = NULL;
        } 

        else {
            NSLog(@"Unknown XPC error.");
        }
    } 
});

xpc_connection_resume(serviceConnection);

and then to send the message

xpc_object_t message = xpc_data_create(NULL, 0);
....
xpc_connection_send_message(serviceConnection, message);

Is there anything I'm missing here? Is it somehow failing to find and connect to the service?

Peter Jacobs
  • 1,657
  • 2
  • 13
  • 29
  • Is there anything in the debugger console? – Peter Hosey Dec 13 '11 at 15:38
  • If there's anything at all related to XPC, it may be worth including in the question anyway. – Peter Hosey Dec 14 '11 at 06:59
  • 4
    Just figured it out. Apparently `message` must be of type `XPC_TYPE_DICTIONARY`, so if I replace `xpc_object_t message = xpc_data_create(NULL, 0)` with `xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);` it works! – Peter Jacobs Dec 14 '11 at 11:17
  • Just as a future tip. if you use any other c api's i.e. xpc_main etc... if you drop to terminal and just type `man xpc_main` or whatever it usually has "running OS specific help docs". Loads of people overlook these as they rarely change, but things like XPC etc. change regularly :) – Adrian Sluyters Jan 13 '16 at 07:27

0 Answers0