%new_copy and similar macros in swigmacros.swg use plain operator new in C++ mode which throws std::bad_alloc when it fails to allocate memory, since this macro is used in typemaps this exception won't be caught and will crash the process.
In C mode this macros use malloc and friend which will simply return NULL, which will cause SWIG to return None,nil or some such in most languages, while not the best behaviour (It should raise a MemoryError or something similar) at least it doesn't crash.
This still seems to be the case for current SWIG versions.
Perhaps SWIG should specify non-throwing
new
- e.g.:And add
#include <new>
too.Then an allocation failure will return
NULL
likemalloc()
does.I've opened a PR with that change: https://github.com/swig/swig/pull/2644