Exception thrown when calling NSConnection connectionWithReceivePort:sendPort:
Exception thrown when calling NSConnection connectionWithReceivePort:sendPort:
- Subject: Exception thrown when calling NSConnection connectionWithReceivePort:sendPort:
- From: Chris Backas <email@hidden>
- Date: Tue, 22 Jul 2008 11:46:48 -0400
Hello all,
This is a little long, the error is summarized at the bottom for
anyone who wants to skip ahead though ;)
I have an application which is using Distributed Objects to ask a
remote machine to obtain and interact with certain resources on its
behalf. This has been working great so far, but recently I've been
trying to add a new feature to the application. It needs to contact
the remote resource over DO, perform some actions, then take down DO
(Because it will be going off-LAN temporarily), perform some actions,
THEN reconnect to DO and finish with the remote resource.
It's the 'reconnect' part that I'm having trouble with. Let me share
some code first though, the error will be at the end.
Here is the code I'm using to connect DO and obtain the remote root
proxy:
-(id)getServerProxy
{
id ReturnMe = nil;
@try
{
// Establish a connection to the server
DOConnectionSocket = [[NSSocketPort alloc] initRemoteWithTCPPort:
[self getHostPort] host:[self getHost]];
DOConnection = [[NSConnection connectionWithReceivePort:nil
sendPort:DOConnectionSocket] retain];
// If the connection was succesful..
if (DOConnection != nil)
{
// Setup connection parameters
[DOConnection enableMultipleThreads];
// Allow a good long time for lengthy DB queries to complete
[DOConnection setRequestTimeout:60];
[DOConnection setReplyTimeout:60];
// Register to be notified if the connection dies
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(connectionDidDie:)
name:NSConnectionDidDieNotification object:DOConnection];
}
ReturnMe = [DOConnection rootProxy];
}
@catch(NSException* error)
{
// Can't do much. Possibly a timeout?
NSLog(@"Failed to get Server Proxy! %@",error);
ReturnMe = nil;
}
return ReturnMe;
}
Here's the code I use to shut down DO:
-(void)disconnectDO
{
connectionOpened = NO;
@synchronized(self)
{
// Clean up and get rid of both proxy objects, but hang onto our
connection ID
// So that we can seamlessly reconnect if possible
[serverProxyObject release]; serverProxyObject = nil;
[connectionProxyObject release]; connectionProxyObject = nil;
// Take down the connection too
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSConnectionDidDieNotification object:DOConnection];
[DOConnection invalidate];
[DOConnectionSocket invalidate];
[DOConnection release]; DOConnection = nil;
[DOConnectionSocket release]; DOConnectionSocket = nil;
}
}
So... the problem is that when I try to reconnect to DO, meaning, call
getServerProxy the second time, I get an exception from the
DOConnection = [[NSConnection connectionWithReceivePort:nil
sendPort:DOConnectionSocket] retain]; line.
The exception:
*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil
The backtrace:
#0 0x971f30d7 in objc_exception_throw ()
#1 0x94fc3f2b in +[NSException raise:format:arguments:] ()
#2 0x94fc3f6a in +[NSException raise:format:] ()
#3 0x900ea3d0 in _NSArrayRaiseInsertNilException ()
#4 0x90008a04 in -[NSCFArray insertObject:atIndex:] ()
#5 0x90008914 in -[NSCFArray addObject:] ()
#6 0x90027d2d in -[NSConnection addRunLoop:] ()
#7 0x90027b8f in -[NSConnection initWithReceivePort:sendPort:] ()
#8 0x90042df7 in +[NSConnection connectionWithReceivePort:sendPort:] ()
#9 0x001ecae7 in -[FourDForwarder getServerProxy] (self=0x6be210,
_cmd=0x1f401c)
*snip*
The exception is internal, but I have to think that it's occurring
because of some state I'm not appropriately cleaning up when I
disconnect. I just can't think what that would be. It's related to
runloops based on the backtrace, and the NSConnection documentation
says that it tries to register with the "Current" run loop.
NSRunLoop's documentation says that if you ask for the current run
loop and there isn't one, one will be created though... So I can't see
a situation where it would be trying to add a nil run loop.
Any ideas?
Thanks!
Chris Backas
_______________________________________________
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