I would like to be able to make SWIG use malloc() for memory allocation in typemaps (using %new_instance, et. al), even when compiling in C++ mode. The reason is basically that malloc() is easier to replace with a custom allocation function than new(). For example, if I have a custom string type that needs to be allocated using a custom allocation function, I can do something like:
#define malloc MyStringMalloc
%fragment("SWIG_FromMyStringPtrAndSize" ...
%typemaps_string(...)
#undef malloc
and still re-use SWIG's typemaps for strings using MyStringMalloc() instead of malloc().
The attached one-line patch to Lib/typemaps/swigmacros.swg makes SWIG use new() for memory allocation only if the symbol SWIG_CPLUSPLUS_USE_MALLOC is not defined. This symbol can then be defined on the command line to make SWIG use malloc() instead. The default behaviour remains the same.
Patch to SWIG 2.0.5
I've found a much better way to do what I was trying to do, so have no further need for this patch