I've been using Cycles as a library inside my own project for years, building it on Windows (MinGW64), Linux (gcc) and macOS Sierra (clang) without any issue. I've recently migrated to Mojave and updated all Cycles code to the very last commit, end of February 2020. On Sierra, after the update, it still compiles and works perfectly as ever, but on Mojave and Catalina it crashes randomly. On Windows and Linux it runs perfectly.
Initially, I blamed the new version of Xcode, but if I compile on Sierra (with the latest code of cycles) and then move the application to Mojave, I still experience random crashes.
It seems the crash appears when I create and destroy a session (either working in background or directly on screen) multiple times. I spent one month to dig out the problem and I found one single line of code which causes the random crash, the line which creates the array data1 in the file bvh.cpp, see snippet below.
I fixed the issue by moving the line which creates the array data1 as shown in the patch.
The code in both snippets does exactly the same thing, there is no functional change. The only difference is that data1 is created only when it is used. If I use a layout BH2, data1 is not required but in the original code it is created anyway.
I cannot really explain why the random crash happens on Mojave and Catalina only, I think it is due to some concurrency issue while sessions are created and destroyed one after the other in close sequence. Considering that both snippets of code are equivalent and that the new code avoids the creation of the array data1 when not required, the change seems to me a reasonable one with a small gain in performance when using a BH2 layout. It definitely fixes the random crash I've experienced on macOS Mojave and Catalina.