views:

55

answers:

2

In my Cocoa application I need to run a task that uses unstable unfixable code. It takes little input, works independently from the rest of the app, and generates thousands of ObjC objects as a result.

How can I run the unstable part and let it crash without taking down whole application?

Is it possible to fork() Cocoa application? How UI, threads, GC, KVO, runloops are going to behave when forked?

Can I avoid creating standalone executable launched via NSTask?

If I launch separate process, how can I send and receive ObjC object instances? (I'd rather not serialize/unserialize them myself, and I need to keep them after child process ends).

How does OS X handle this problem for Spotlight and Quicklook plugins?

+1  A: 

I use Distributed Objects to communicate between my cocoa program and a separate (unreliable) worker program. I start the worker as a NSTask. Distributed objects are very elegantly put together.

neoneye
Distributed Objects documentation states that when connection is severed, the objects cease to exist. I'd like task to end quickly, but keep the objects in the main process. How can I do that?
porneL
cease to exist... I'm not sure exactly what you are asking. My task is long lived and I can invoke methods on it via DO without problems. DO will serialize/unserialize every request, so the code looks exactly as if I were interacting with an object within the same process. If your task is very short lived then perhaps just use STDIN and STDOUT for IPC, a great example of this is: http://developer.apple.com/mac/library/samplecode/Moriarity/Introduction/Intro.html
neoneye
+2  A: 

Is it possible to fork() Cocoa application?

Yes, but you pretty much have to exec immediately. Core Foundation will throw an exception if you try to use certain Cocoa methods or CF functions between fork and exec (or without execking at all). You might get away with some things (I was able to ask a window its frame, for example), but nothing is safe.

Launching an NSTask, of course, counts as fork and exec together, averting the problems of skipping or postponing the exec.

How UI, threads, GC, KVO, runloops are going to behave when forked?

UI: Windows (the actual ones on the screen) are not duplicated. Of course, you can't talk to your NSWindow and NSView objects anyway.

Threads: Not carried over to the subprocess. This is not as good as it may sound, as problem cases abound; for one, another thread might have held a lock in the parent, which remains locked in the child even though the thread that held it is absent.

GC: Well, the garbage collector runs on a thread…

KVO: Should be fine, since observation is normally triggered either explicitly or by KVO-supplied wrapper accessors.

Run loops: One per thread, so the main thread's run loop should still exist, but it will die if you return to it.

Can I avoid creating standalone executable launched via NSTask?

Nope.

If I launch separate process, how can I send and receive ObjC object instances?

If you don't exec, you don't.

Otherwise, you can use DO.

(I'd rather not serialize/unserialize them myself, and I need to keep them after child process ends).

Then you'll need to make a copy in the parent process. I don't know whether you can use copyWithZone: here; probably not. I suspect you will have to do some sort of plist- or archive-based serialization/unserialization.

How does OS X handle this problem for Spotlight and Quicklook plugins?

Spotlight has mdworker; Quick Look has something similar.

Peter Hosey
Also.. posix_spawn() is a modern alternative to fork/exec. I think NSTask is using it internally.
neoneye
Thanks for detailed answer. So to sum it up: `NSTask` + `NSKeyedArchiver` is the way to go?
porneL
I'd say so. Note that if you want to send instances of custom classes from either side, you'll need to implement NSCoding in those classes.
Peter Hosey