16

I have a very complicated class for which I'm attempting to make Python wrappers in SWIG. When I create an instance of the item in Python, however, I'm unable to initialize certain data members without receiving the message:

>>> myVar = myModule.myDataType()
swig/python detected a memory leak of type 'MyDataType *', no destructor found.

Does anyone know what I need to do to address this? Is there a flag I could be using to generate destructors?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
  • 1
    Related question: http://stackoverflow.com/questions/13587791/swig-and-c-memory-leak-with-vector-of-pointers/13593596#13593596 – Flexo Nov 29 '12 at 16:31

2 Answers2

12

SWIG always generates destructor wrappers (unless %nodefaultdtor directive is used). However, in case where it doesn't know anything about a type, it will generate an opaque pointer wrapper, which will cause leaks (and the above message).

Please check that myDataType is a type that is known by SWIG. Re-run SWIG with debug messages turned on and check for any messages similar to

Nothing is known about Foo base type - Bar. Ignored

Receiving a message as above means that SWIG doesn't know your type hierarchy to the full extent and thus operates on limited information - which could cause it to not generate a dtor.

ASk
  • 4,157
  • 1
  • 18
  • 15
  • 2
    Which SWIG debug flag should be turn on? Some of them produce awful lot of information. Under which flag should I be looking for messages you mentioned? – Michal Nov 29 '17 at 02:16
-13

The error message is pretty clear to me, you need to define a destructor for this type.

hhafez
  • 38,949
  • 39
  • 113
  • 143
  • 3
    This isn't correct. It's entirely possible to have defined a destructor, or be using a default default destructor and not see this error. The important thing is that SWIG knows about the existence of such a destructor. See: http://stackoverflow.com/questions/13587791/swig-and-c-memory-leak-with-vector-of-pointers/13593596#13593596 – Flexo Nov 27 '12 at 23:05