4

This is about embedded python using swig.

I have an std::map<enum, std::string> exposed to python (embedded python). When the script is executed, swig spits out the below "warning" at the end (when the map goes out of scope - I guess):

swig/python detected a memory leak of type 'std::map< MyEnum, std::string>
... no destructor found

The .i file is:

enum MyEnum {
 ...
};

typedef std::map<MyEnum, std::string>  MyTypedef;
%template(MyTypedef) std::map<MyEnum, std::string>;

Things are ok if I replace MyEnum with int all over the code. Don't know why swig needs any special destruction when enums are not PyObjects! Am I missing something? Is there some %magic_operator which will help.
Note: I do not want to suppress the "memory leak" warning all together.

Took a hard look at the wrapper generated by swig but in vain.

taras
  • 6,566
  • 10
  • 39
  • 50
sambha
  • 1,333
  • 3
  • 17
  • 30
  • Does SWIG know the full definition of the enum? Check the answers to http://stackoverflow.com/questions/918180/swig-python-memory-leak-detected – Wynand Winterbach Jan 10 '11 at 18:01

1 Answers1

0

Could be a problem with declaration order: the std::map %template must be declared AFTER MyEnum is added to the SWIG interface.

I have failed to do this in the past (though in my case the map key was a typedef alias to an unsigned int), and saw the exact same set of symptoms reported here.

  • 1
    The std::map %template ARE mentioned AFTER MyEnum as shown above. So, looks like there is something more to this. Note that this happens only for Enums in maps. An int-to-string map is all good. – sambha Mar 17 '11 at 03:32