views:

767

answers:

4

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?

Thanks for any help you can offer!

+1  A: 

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

hhafez
A: 

For reasons specific to the project, our classes have no methods. They're effectively structs. Given that I am not permitted to add any function (including a destructor), do I have any options available to me?

I also find it strange that while -none- of my classes have a constructor or destructor, only some of them bug out with this "memory leak detected!" message. Why wouldn't it be consistent?

Zack
Do the classes that are affected contain pointers to other class objects? It might be detecting this and complaining that the class has no way to free the object being pointed to when the container object is freed.
jkf
I believe they contain either structs or unions in each instance. Each class is designed to be self-contained -- there aren't (to the best of my knowledge) any pointers to the world outside each object.Thanks for the thought, though!
Zack
+2  A: 

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
A: 

[Apologies for answering rather than commenting -- I just registered formally so it doesn't recognize me as the original poster and I don't have the reputation points.]

Thanks very much, ASk. I've been running Swig with the -Wall flag and it doesn't give me any warnings. I also tried using the debug flags for classes and typedefs and still didn't see anything that looked alarming.

Honestly, I could live with the memory leak -- I'm only concerned with it because I suspected that it might be the cause of another, more critical problem I was having.

Do you happen to have any other suggestions? Much obliged for all your assistance thus far!

Zack
You can always extend your struct in the swig wrapper itself, with a destructor.
ASk