fix segfault on exit.
potentially add a shutting_down bool to the c++ layer.
add all shutting_down failsafe checks in c++.
add SHUTTING_DOWN macro instead.
try the shutting down check in c++.
Revert "try the shutting down check in c++." - does not work.

This reverts commit bf6b4d9b60c89520438ec5232c2aed8740fe9e4b.