python - 检测到内存泄漏...未找到析构函数 - 对于枚举

标签 python swig

这是关于使用 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/

相关文章:

python - Cython 并行计算中可变数量的 worker

python - 如果在python中发生错误,则处理异常

c++ - 如何将 boost::tuples::tuple 暴露给 Java 绑定(bind)?

node.js - 如何更改 .twig 的 node.js swig 模块扩展名

python - python包中的对象是什么?

python - Pyplot - 自动将 x 轴范围设置为传递给绘图函数的最小、最大 x 值

Python requests.post 证书验证失败

python - SWIG 安装升压错误

c++ - 为 SWIG 指定 Python header 和库

java - 如何避免在 Java 和 native C++ 代码之间复制数据