Re: NSTask objectalloc problem
Re: NSTask objectalloc problem
- Subject: Re: NSTask objectalloc problem
- From: Michael Ash <email@hidden>
- Date: Thu, 29 Apr 2010 19:32:17 -0400
On Wed, Apr 28, 2010 at 2:16 PM, Tom Foutz <email@hidden> wrote:
> I am trying to run a back-up type of unix script whenever a file
> changes in the directory. However, my implementation, when viewed in
> "Instruments" with "ObjectAlloc", demonstrates an ever-increasing
> number of living objects.
>
> A problematic dummy version:
> ##############################################
> #import <Foundation/Foundation.h>
> void sync() {
> NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
> // Dummy task is just to use unix /bin/ls command
> NSTask *ls;
> ls = [[NSTask alloc] init];
> [ls setLaunchPath:@"/bin/ls"];
> [ls launch];
> [ls release];
> [pool drain];
> }
>
> int main (int argc, const char * argv[]) {
> NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
> int i;
> for (i=1; i<=5e4; i++) {
> sync();
> }
> [pool drain];
> return 0;
> }
> ##############################################
>
> I found this old message, which mentions fixing the problem by using
> an NSThread:
> http://lists.apple.com/archives/cocoa-dev/2006/Nov/msg01415.html
>
> However, imagine if my dummy task (ls) took five minutes, and in the
> mean time, another change occurs on the directory, I am afraid that
> another sync job would be initiated on another thread. Perhaps one
> solution would be to keep track of the thread instance, and to not
> create another NSThread job if the first had not yet finished?
NSTask has to track the subtask until it terminates, even if your code
is done with the object. To do this, the object will wait around in
the background listening for the termination notification.
I believe that the problem is that listening for this notification
requires an active runloop, which you don't have.
To fix it, run the NSRunLoop, one way or another. You could do this by
using runUntilDate: in your while loop, by replacing your while loop
with an NSTimer and using -run, or by taking Jens's suggestion and
using -waitUntilExit.
Mike
_______________________________________________
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