我有一个代码,它用c++进行一些计算,然后使用增强.python(和增强.numpy). 更确切地说,有一个简化的例子。首先它是一个函数
namespace np = boost::python::numpy;
std::pair<np::ndarray, np::ndarray> f() {
const auto dt = np::dtype::get_builtin<double>();
np::ndarray a = np::empty(a_shape, dt);
np::ndarray b = np::empty(b_shape, dt);
// filling arrays
return {std::move(a), std::move(b)};
}
返回一对numpy数组。还有一个我导出到python的类:
namespace p = boost::python;
class WrappedClass {
public:
p::dict GetData() {
p::dict result;
for (const std::string& key : keys_) {
auto arrays = f();
result[key] = p::make_tuple(std::move(arrays.first), std::move(arrays.second));
}
return result;
}
};
一切正常:我从python调用GetData()
方法并获得正确的数据。问题是,我创建的所有numpy数组(可能还有dict)永远不会被删除,即使python代码中对它们的所有引用都被删除了(调用gc.collect()
也没有任何帮助)。在python中对这个实例执行del
操作时,会调用WrappedClass
实例的析构函数。你知道吗
从c++代码返回python对象以避免内存泄漏的正确方法是什么?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐