这是关于使用 swig 的嵌入式 python。
我有一个 std::map<enum, std::string>
暴露于 python(嵌入式 python)。执行脚本时,swig 在最后吐出下面的“警告”(本地图超出范围时 - 我猜):
swig/python detected a memory leak of type 'std::map< MyEnum, std::string>
... no destructor found
.i 文件是:
enum MyEnum {
...
};
typedef std::map<MyEnum, std::string> MyTypedef;
%template(MyTypedef) std::map<MyEnum, std::string>;
如果我替换 MyEnum
就没问题了在代码中使用 int。当枚举不是 PyObjects 时,不知道为什么 swig 需要任何特殊的破坏!我错过了什么吗?有没有%magic_operator
这会有所帮助。
注意:我不想一起抑制“内存泄漏”警告。
仔细看了看swig生成的wrapper,但是没用。
最佳答案
可能是声明顺序的问题:std::map %template 必须在 MyEnum 添加到 SWIG 接口(interface)之后声明。
我过去没能做到这一点(虽然在我的例子中映射键是 unsigned int 的 typedef 别名),并且看到了与此处报告的完全相同的一组症状。
关于python - 检测到内存泄漏...未找到析构函数 - 对于枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3989859/