boost.pythonの引数としてポインタを使用してpythonの関数を呼び出すと、デストラクタにいくつかの問題があります。以下はサンプルコードです
c ++
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <string>
#include <iostream>
using namespace boost::python;
class A {
public:
A() {
std::cout<< "A start"<<std::endl;
}
~A() {std::cout<< "A end" <<std::endl;}
}
class B {
public:
B() { aa=new A; }
~B() { delete aa; }
void run(object ct) {
_obj=ct(); //creat a python object
_obj.attr("fun1")(aa); //call a function named "fun1" with a pointer arg
_obj.attr("fun2")(aa); //call a function named "fun2" with a pointer arg
}
A *aa;
object _obj;
}
BOOST_PYTHON_MODULE(ctopy)
{
class_<A> ("A",init<>())
;
class_<b> ("B",init<>())
.def("run",&B::run)
;
}
Python:
import ctopy
class tc:
def fun1(self,tt):
print "fun1"
def fun2(self,tt):
print "fun2"
bb=ctopy.D()
bb.run(tc)
この結果:
A start
fun1
A end
fun2
A end
A end
ノート:
「Aend」が3つ印刷されています。「valgrind」で試してみましたが、エラーがあります。デストラクタを1回だけ実行したいのですが、どうすればよいですか?