NSRunLoop, processes and distributed objects
NSRunLoop, processes and distributed objects
- Subject: NSRunLoop, processes and distributed objects
- From: Felix Schwarz <email@hidden>
- Date: Thu, 24 Nov 2005 18:10:26 +0100
Hi,
I'm having a problem with an NSRunLoop that does not exit. It
includes the use of processes and distributed objects.
But first things first. My application launches a new process via
NSTask, that then vends an object. The application finds that object,
makes use of it, etc. However, when my application stops using the
NSTask and even invalidates the NSConnection receive and send ports
at the core of the distributed object used here, the runloop does not
exit. It does not even exit if, in the seperate process, it is told
to runUntilDate.
In the "Multithreading Programming Topics" part of the Cocoa docs, I
found this:
-- cut --
Exiting a Thread’s Run Loop
When you execute a run loop, the thread enters a permanent loop,
during which it processes events associated with the attached input
sources and timers. If you want to terminate a thread once it has
entered its run loop, there are several ways to do it. The most
graceful way to exit is to remove all timers and input sources from
the run loop. Without any event sources, the run loop exits
naturally, letting your thread entry point function end normally.
This is the preferred way for Cocoa threads to exit as it gives them
a chance to delete their autorelease pools.
..
-- cut --
While this is on threads I'd assume that the same is true for
runloops in general. Since my process does not have any windows and
all it does really is to vend this object, what does hinder the
NSRunLoop from exiting even after the connection by my main
application process was quit?
Investigating further on this topic, I found a note in the code
sample SimpleThreads, that gives me a hint, that I might have to do
more:
-- cut --
/*
TIMC
Currently, this routine does not work properly. Both connection
objects seem to be retained many more times than necessary -- and the
number goes up as you make server calls. So, this is close to what
is needed, but clearly something else is going on in DO.
*/
- (void)killThreads: (id)sender
{
TransferServer *server;
NSConnection *connect;
int loop;
loop = [freeServers count];
while (loop > 0)
{
server = [freeServers objectAtIndex:loop-1];
[freeServers removeLastObject];
connect = [(id) server connectionForProxy];
// we invalidate our send port, which will invalidate the connection
in the
// threaded object, causing its runloop to abort and the thread to
exit.
[[connect receivePort] invalidate];
// release the connection, we're done with it
NSLog([NSString stringWithFormat:@"Connection being disposed,
retain count is ld\n", [connect retainCount]]);
[server release];
[connect release];
loop--;
}
};
-- cut --
But, of course, the code sample will not tell me what I'll have to do.
I'd be very thankful for any advice or insight as to what might be
going wrong. I really want the NSTask to exit naturally, not -
terminate it.
Thanks in advance.
Felix _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden