【发布时间】:2013-09-23 13:32:01
【问题描述】:
我正在尝试使用boost::shared_ptr 来允许我在我的python 脚本中使用c++ 文件I/O 流对象。但是,生成的包装器警告我它正在泄漏内存。
这是一个显示问题的最小.i 文件:
%module ptrtest
%include "boost_shared_ptr.i"
%include "std_string.i"
%shared_ptr( std::ofstream )
%{
#include <fstream>
#include <boost/shared_ptr.hpp>
typedef boost::shared_ptr< std::ofstream > ofstream_ptr;
ofstream_ptr mk_out(const std::string& fname ){
return ofstream_ptr( new std::ofstream( fname.c_str() ) );
}
%}
ofstream_ptr mk_out(const std::string& fname );
%pythoncode %{
def leak_memory():
''' demonstration function -- when I call
this, I get a warning about memory leaks
''''
ostr=mk_out('/tmp/dont_do_this.txt')
%}
这是警告:
In [2]: ptrtest.leak_memory()
swig/python detected a memory leak of type 'ofstream_ptr *', no destructor found.
有没有办法修改.i文件告诉接口如何正确处理shared_ptr?
【问题讨论】:
-
刷新以查看带有完整、最小示例且易于回答的问题 :)
标签: c++ python file-io memory-leaks swig