简体   繁体   中英

Detected a memory leak… no destructor found - for enums

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM