• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Running an NSTask Within an NSThread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Running an NSTask Within an NSThread


  • Subject: Re: Running an NSTask Within an NSThread
  • From: James Bucanek <email@hidden>
  • Date: Sun, 27 Jan 2008 08:37:41 -0700

Jonathan Dann <mailto:email@hidden> wrote (Sunday, January 27, 2008 7:09 PM -0000):
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?

Yes. A running system consists of a number of processes. Each process has its own memory, I/O, process ID, etc. Each process consists of one or more threads. Each thread shares the memory, I/O, process ID, etc. with the other threads running in the same process.


What would be any possible disadvantage?

Not too many, except maybe the one's you're having. ;)

It's mostly a matter of program organization. The NSTask is a separate process so it's already running independently from the threads in your process. I often spawn a new thread for my NSTasks because it makes them easier to write. For instance, I can start an NSTask and then just call [NSTask waitUntilExit] (so it works kind of like join()), rather than trying to catch the NSTaskDidFinishNotification.

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.

No, it's not the same thing. The process will run in a separate memory space in its own thread regardless of how you do this because that's the definition of an NSTask. Whether you start the NSTask from thread A or thread B in your application is entirely up to you, but doesn't change how the other process will run.


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?

Without seeing the rest of your code, I'd have to guess that your NSTask is underretained and is getting released prematurely. From the NSTask documenation:


    NSTaskDidTerminateNotification

Posted when the task has stopped execution. This notification can be
posted either when the task has exited normally or as a result of
terminate being sent to the NSTask object. If the NSTask object gets
released, however, this notification will not get sent, as the port the
message would have been sent on was released as part of the task release.


My guess is that with a single thread you're releasing the object and the notification is trying to use a released object. In the multithreaded case the NSTask object is getting released earlier and you're not even getting the notifications.

Anyway, garbage collection (Leopard) or NSZombies (Tiger) are your friends here. Garbage collection would probably solve the problem. NSZombies and/or ObjectAlloc would allow your to track down the problem.

--
James Bucanek

_______________________________________________

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


References: 
 >Running an NSTask Within an NSThread (From: Jonathan Dann <email@hidden>)

  • Prev by Date: Re: Locate path to file's directory
  • Next by Date: Help about the Interface builder core animation function
  • Previous by thread: Running an NSTask Within an NSThread
  • Next by thread: Re: Running an NSTask Within an NSThread
  • Index(es):
    • Date
    • Thread