Swig / Python memory leak detected

2019-02-21 11:36发布

问题:

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?

回答1:

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.



回答2:

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