Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Running an NSTask Within an NSThread



Hi,

I'm writing an app that needs to run a fairly lengthy background process, but the process itself is an NSTask. Does the task itself run on its own thread anyway or am I better spawning a new thread from my main one? What would be any possible disadvantage? The docs say that a task runs in a separate memory space to the calling app, but I don't know if that's the same thing.

I've tried both ways, and when spawning a thread I have a method that calls [task launch], the method then finished its execution and therefore the thread finishes. I then want to evaluate something when the NSTaskDidFinishNotification is posted (for which my class is an observer). Here's the current catch:

When I don't span a new thread I get NSLogs showing that two calls to my -taskDidFinish:(NSNotification *)note method and a crash. When I spawn a thread, I get no calls to the -taskDidFinsh method, therefore I have a memory leak! Any ideas?

Here's my code

- (void)runTask;
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	task = [[NSTask alloc] init];
	// configure task & call some cocoa calls with autoreleased objects
	
	@try {
	[task launch];
	}
	@catch // blah blah
	}
	[pool release];
}

- (void)taskDidTerminate:(NSNotification *)note;
{
	NSLog(@"-taskDidTerminate:");
	[task release];
	// do more stuff with output of task
}

So think that for some reason I'm getting my task released twice, but I don't know why the taskDidTerminate method is called twice. I don't get this crash when I spawn another thread, could the thread be crashing silently?

Hope this is clear enough, thanks.

Jon
_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.