Re: NSTask not cleaning up it's threads
Re: NSTask not cleaning up it's threads
- Subject: Re: NSTask not cleaning up it's threads
- From: "C. Tearpak" <email@hidden>
- Date: Wed, 15 Oct 2008 01:25:42 -0700
Thanks for your insight, Ken. It's very possible that you are right,
though I haven't tried your suggestions, especially since you are
mentioning the "wait" parameter. I am normally a C# / .NET programmer
by day, with other experience in Java, so the RunLoop and Timer are
still somewhat foreign concepts to me, and when looking at threading,
having control via the NSThread is more familiar and less weird to me.
Hopefully, that will change, as I have seen APIs that require doing
work via RunLoops and I have run in to issues when an NSThread created
by me needs to interact with one.
I seem to have solved the problem. It seems like, after some
tinkering, adding a [task waitUntilExit]; before exiting the
runCommand method fixes the thread leak.
I suppose it could be the fact that NSTask is "creating a separate
executable entity" per the NSTask documentation, and that, even though
it does not share memory space with my process, it would need to
communicate via the pipe that was configured for stdout. If the
objects created in that method were cleaned up and it could no longer
communicate via the pipe or whatever, it might just leak and not know
how or when to terminate itself.... That's what it seems like, anyhow.
If anyone has any insight as to the actual reason that this would be
happen and could shed light, I would definitely like to know what is
actually going on to gain a better understanding of the underlying
APIs and their interactions with executables and the environment.
--Chris
On Oct 14, 2008, at 10:46 PM, Ken Thomases wrote:
On Oct 14, 2008, at 6:43 AM, C Tearpak wrote:
The following is a test application that demonstrates what I am
seeing. If
you look at the application in Activity monitor, you will see the
threads go
up every second.
Is there something that I am missing? For a long-running
application that
has the following in a thread loop, this causes a huge issue, as I
can't
seem to get these threads to clean up. I have also tried dealloc(),
setting
the pointers to nil and NULL, and calling -terminate: on the NSTask
before I
exit the runCommand:: method;
[...]
int main (int argc, const char * argv[]) {
mainTest* mainT = [mainTest new];
while(true)
{
NSLog([mainT runCommand: @"/bin/date" withArguments:[NSArray new]]);
sleep(1);
}
Have you tried using a run loop and a timer to perform this
operation once a second, rather than a while loop and sleep()?
I suspect that the threads being created by NSTask and/or
NSFileHandle are trying to interact with the main thread using
something like -performSelectorOnMainThread:... with YES for the
"wait" parameter. That requires that the main thread's run loop
gets run in the specified mode (likely the default mode).
You can use Activity Monitor's Sample Process feature to get a sense
of what the extraneous threads are stuck waiting for.
Cheers,
Ken
_______________________________________________
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