objective-cexceptiondistributed-objectsnsconnection

Objective-C: Avoiding EXCEPTIONS when NSConnection to Distributed Objects fail


I'm using Objective-C Distributed Objects (DO) to share data from one application (that collects data from the network) to another (a patch inside Quartz Composer). When the connection to the distant object fails (when I shut down the first application), I'm get:

5/16/12 8:17:06.373 PM Quartz Composer: *** EXCEPTION IGNORED: connection went invalid while waiting for a reply because a mach port died

After that point, the Quartz composition is hung. Even after I bring the first application back up, it's still hung. I want the Quartz patch to reconnect.

I am using the Notification Center to put shut down the old objects, like this:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(connectionDidDie)
                                             name:NSConnectionDidDieNotification
                                           object:theConnection];

Right now, my counnectionDidDie looks like this:

- (void) connectionDidDie
{
    NSLog(@"Connection died and we detected it");
    [[self proxyObject] release];
    [self setProxyObject:nil];
    theConnection = nil;
}

I also check to be sure the connection is still alive, just before accessing any part of the proxyObject, like this:

if ([NSConnection defaultConnection]) { // this line triggers the exception
    // access proxyObject
}

I've also tried

if ([theConnection isValid]) { // this line triggers the exception
    // access proxyObject
}

In both cases, it's this test that triggers this EXCEPTION.

What can I do to prevent the hang of Quartz when I shutdown the first application, who has the vended object?


Solution

  • I was never able to find a way to shut down the DO connection fast enough to prevent QC, who draws as 30-60 frames per second, front testing the connection (and crashing) before connectionDidDie: was called. Ultimately, I've decided to use DO, only to get an initial copy of the object, which then do a deep copy of, and then kill the DO connection while nothing is trying to access it. DO just doesn't seem to be that great of solution, after you dig into it. :(