Deallocating Distributed Objects
Deallocating Distributed Objects
- Subject: Deallocating Distributed Objects
- From: John Nairn <email@hidden>
- Date: Thu, 22 Aug 2002 10:02:29 -0600
I am trying to set of distributed objects for multithreading. It is
working OK, but I can not clean up when they are closed. Any suggestions
would be appreciated - it is a major road block in my application which
really should be multithreaded.
1. The controller is a window controller in a document and the server is
a separate object. When the document closes, I try to close the
controller, thread, and connections with following method in the
controller
// called from dealloc in document controller
- (void)killThread
{
NSLog(@"killThread: controller/server retains: %d/%d",[self
retainCount],[runServe retainCount]);
[[connectionToRunServer receivePort] invalidate];
NSLog(@"killThread: controller/server retains: %d/%d",[self
retainCount],[runServe retainCount]);
[runServe release];
[connectionToRunServer release];
}
Initially the controller and server retains are 2/1. After invalidating
the receive port, the controller drops to retain of 1 and server stays
at 1. It all looks OK. The two problems are:
a. The [runServe release] never results in its dealloc method being
called (despite the fact the retain should be going to zero)
b. The initial thread in the server has a run loop which never exits and
thus its autorelease pool is never released.
2. To complete the picture, the server is initially set up with code
essentially identical to SimpleThreads sample code from Apple. The
specifics are:
// in the controller to set up connections
- (void)openServer
{
NSPort *port1;
NSPort *port2;
NSArray *portArray;
long waitCounter;
port1=[NSPort port];
port2=[NSPort port];
connectionToRunServer=[[NSConnection alloc]
initWithReceivePort:port1 sendPort:port2];
portArray=[NSArray arrayWithObjects:port2, port1, nil];
[NSThread detachNewThreadSelector:@selector(connectWithPorts:)
toTarget:[RunServer class]
withObject:portArray];
waitCounter=0;
while([connectionToRunServer rootProxy]==nil)
{ waitCounter+=1;
NSAssert(waitCounter<10000000,@"TransferServer did not set up
rootProxy quickly enough.");
}
[[connectionToRunServer rootProxy]
setProtocolForProxy:@protocol(RunServerInterface)];
runServe=(RunServer *)[connectionToRunServer rootProxy];
[runServe retain];
}
// ket thread in the server
+ (void)connectWithPorts:(NSArray *)portArray
{
NSAutoreleasePool *pool;
NSConnection *connectionToController;
RunServer *serverObject;
TextController *tc;
pool=[[NSAutoreleasePool alloc] init];
connectionToController=[NSConnection
connectionWithReceivePort:[portArray objectAtIndex:0]
sendPort:[portArray
objectAtIndex:1]];
tc=(TextController *)[connectionToController rootProxy];
serverObject=[[self alloc] init];
[connectionToController setRootObject:serverObject];
[serverObject release];
[[NSRunLoop currentRunLoop] run];
NSLog(@"thread runLoop stopped"); // never occurs
[pool release];
}
----------------
John Nairn (1-801-581-3413, FAX: 1-801-581-4816)
Web page:
http://www.mse.utah.edu/~nairn
_______________________________________________
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.