1

I tried to fork() a Cocoa process and setup a new Cocoa/ObjC environment but I get the error:

The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.

Is there a way to re-initialize Cocoa/ObjC in the forked process?


I know that the documentation about fork() does say it is limited, however I wonder if there is still a way to do it, not just what the documentation says.

I tried to read the source code of the XNU kernel which handles the execve syscall (bsd/kern/kern_exec.c) but I am not that used to the code that I cannot easily say wether it is possible or not to do something similar in userspace or not.

Albert
  • 65,406
  • 61
  • 242
  • 386

1 Answers1

3

Mac OS X severely limits what you can do after a fork without execing. See the CAVEATS section of the fork man page. The allowed actions do not include using Cocoa or CoreFoundation, so you must call exec to start a new program.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • I know what the documentation is saying but it doesn't really answer my question wether there might still be a way somehow to do that. I extended my question a bit to show what I have in mind as a possible way. – Albert Sep 11 '11 at 18:14
  • @Albert Related: as you can only rely on async-safe functions after you `fork()` and before you `exec()`, take a look at [Landon’s answer to a PLCrashReporter question](http://stackoverflow.com/questions/7312905/using-plcrashreporter-can-you-receive-events-before-the-crash/7365872#7365872). –  Sep 11 '11 at 19:07