1

I have a big C project and I want to expose it's API to python by using SWIG. For that, I have a script for generating automatically the interface file for swig (.i file). For each type defined in the project, the script adds to the interface file %pointer_functions and %array_functions for that type. Those directives create set of functions including a "new" function which in turn allocates memory. Is there a way to automatically deallocate that memory when the object is garbage collected instead of calling explicitly the "delete" function?

I have a solution for structs, using the %newobject directive and %extend with destructor definition which free the allocated memory - similar to the solution in this post SWIG - Garbage Collection using %newobject . But it doesn't work on primitive types like int, enum, etc... Any ideas how to solve it for such types?

guy szweigman
  • 514
  • 5
  • 8
  • Provide a [mcve] showing how your currently solution leaks. – Mark Tolonen Jun 04 '19 at 17:02
  • It sounds like you might making this more complicated than you have to with SWIG, although it's hard to say for certain with what you've shown. Perhaps have a look at `%array_class` instead. – Flexo Jun 05 '19 at 08:04
  • Agree with @Flexo - more complicated than needed. I would not recommend to start automatically generating interface files before you have found a better alternative. Look into `%array_class` and `%array_functions`. If you have many input and output arrays of primitives, look into `numpy.i` – Jens Munk Jun 05 '19 at 18:04

0 Answers0