Menu

#294 Allow use of malloc() in C++ mode

closed-out-of-date
None
5
2012-03-12
2012-02-09
No

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.

Discussion

  • Anonymous

    Anonymous - 2012-02-09

    Patch to SWIG 2.0.5

     
  • Anonymous

    Anonymous - 2012-02-09
    • assigned_to: nobody --> wsfulton
     
  • Anonymous

    Anonymous - 2012-03-12

    I've found a much better way to do what I was trying to do, so have no further need for this patch

     
  • Anonymous

    Anonymous - 2012-03-12
    • status: open --> closed-out-of-date
     

Log in to post a comment.