I’ve run into an while trying to launch an NSTask from inside an NSOperation.
Here’s a very simple app I’ve put together to showcase the problem. When you click the button, an NSOperation is queued. It sets up an NSRunLoop, and calls a method which invokes an NSTask. The task is really simple- it just launches /bin/sleep for two seconds (enough to easily see the spinner when things are working properly).
The app works as advertised, however if you change line 23 of TaskPerformer.m to an autorelease, (sorry, I’m a new poster so I can’t link directly) or comment it out entirely (thus leaking the NSTask object), the NSOperation‘s thread will never exit. Its main runloop seems to be blocking on something.
Now, the problem here is twofold. First off, I don’t understand why my thread is blocking, but moreover, if I turn on garbage collection for this application, the same behavior manifests itself. Because there’s no way for me to manually release the NSTask, the thread blocks no matter what.
If someone could tell me what’s going on, I’d be eternally grateful!
I figured out what was going on here. Turns out my call to
[runLoop run]was setting up the loop and running it indefinitely. It was never falling down into thewhile (!done)loop. Turns out that after callingrun, anNSRunLoopwill run until it has no more inputs. Callingreleaseon myNSTaskled to exactly this scenario, so (quite by accident) my runloop exited.The solution was to remove
[runLoop run]and simply rely on my ownwhileloop. Hope this helps somebody else!