在C ++静态boost :: shared_ptr中保存python生成的值

时间:2013-08-12 10:00:40

标签: c++ boost shared-ptr boost-python python-bindings

在使用Boost.Python和C ++时,有时我们会创建使用类本身和boost::shared_ptr<>版本绑定的类。由于许多原因,这非常方便,可以在很多地方使用。 但是,当boost::pythonboost::shared_ptr<>返回到在Python中生成并记录在C ++静态变量上的值时,该机制似乎无法可靠地工作。

通常情况下,我希望返回的boost::shared_ptr<>能够保留一个特殊的删除器来处理这个问题,但似乎并非如此。似乎发生的事情是返回的boost::shared_ptr只包含一个指向Python中生成的值的指针,没有任何特殊考虑因素。这会导致双重删除(一个来自Python解释器本身,一个来自C ++静态)的一致崩溃 - 或者至少它看起来像。

要使用以下代码重现此行为,请创建如下所示的test.cc文件,并使用以下脚本进行测试。

#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>

struct A {
  std::string _a;
  A(std::string a): _a(a) {}
  std::string str() { return _a; }
};

static boost::shared_ptr<A> holder(new A("foo"));

static void set_holder(boost::shared_ptr<A> a_ptr) {
  holder = a_ptr;
}

static boost::shared_ptr<A> get_holder() {
  return holder;
}

BOOST_PYTHON_MODULE(test)
{
  using namespace boost::python;

  class_<A, boost::shared_ptr<A> >("A", init<std::string>())
    .def("__str__", &A::str)
    ;

  def("set_holder", &set_holder);
  def("get_holder", &get_holder);
}

使用以下Python测试程序:

import test
print(str(test.get_holder()))
test.set_holder(test.A('bar'))
print(str(test.get_holder()))

在Linux下编译(使用g++ -I/usr/include/python2.7 -shared -fpic test.cc -lboost_python -lpython2.7 -o test.so)并运行上述程序(python test.py)(ubuntu 12.10,使用Python 2.7和Boost 1.50),产生了以下堆栈跟踪:

#0  0x000000000048aae8 in ?? ()
#1  0x00007fa44f85f589 in boost::python::converter::shared_ptr_deleter::operator()(void const*) () from /usr/lib/libboost_python-py27.so.1.50.0
#2  0x00007fa44fa97cf9 in boost::detail::sp_counted_impl_pd<void*, boost::python::converter::shared_ptr_deleter>::dispose() ()
   from /remote/filer.gx/home.active/aanjos/test.so
#3  0x00007fa44fa93f9c in boost::detail::sp_counted_base::release() ()
   from /remote/filer.gx/home.active/aanjos/test.so
#4  0x00007fa44fa9402b in boost::detail::shared_count::~shared_count() ()
   from /remote/filer.gx/home.active/aanjos/test.so
#5  0x00007fa44fa94404 in boost::shared_ptr<A>::~shared_ptr() ()
   from /remote/filer.gx/home.active/aanjos/test.so
#6  0x00007fa450337901 in __run_exit_handlers (status=0, 
    listp=0x7fa4506b46a8 <__exit_funcs>, run_list_atexit=true) at exit.c:78
#7  0x00007fa450337985 in __GI_exit (status=<optimized out>) at exit.c:100
#8  0x00007fa45031d774 in __libc_start_main (main=0x44b769 <main>, argc=2, 
    ubp_av=0x7fffaa28e2a8, init=<optimized out>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffaa28e298) at libc-start.c:258
#9  0x00000000004ce6dd in _start ()

这表示在静态析构函数中发生了双重删除。这种行为似乎在不同平台之间是一致的。

问题:是否可以实现所描述的行为,而无需复制boost::python的返回值?在上面的玩具示例中,这很简单,但在我的实际问题中,A的深层副本将是不切实际的。

1 个答案:

答案 0 :(得分:0)

你遇到的问题是在python完成后会发生shared_ptr的破坏。看看:

我建议封装shared_ptr,它没有额外的清理代码。 但是有四个方面:

    #include <boost/python.hpp>
    #include <boost/shared_ptr.hpp>
    #include <iostream>

struct A {
  std::string _a;
  A(std::string a): _a(a) {}
  ~A() { std::cout << "Destruct: " << _a << std::endl; }
  std::string str() { return _a; }
};

void python_exit();

static boost::shared_ptr<A> holder(new A("foo"));

static boost::shared_ptr<A> get_holder() {
  return holder;
}

static void set_holder(boost::shared_ptr<A> a_ptr) {
  // The shared_ptr comes with python::converter::shared_ptr_deleter
  holder = a_ptr;
}


// Fix 1: Cleanup while python is running
// ======================================

void reset_holder() {
   std::cout << "reset" << std::endl;
   holder.reset(new A("holder without shared_ptr_deleter"));
}


// Fix 2: The shared pointer is never deleted (which is a memory leak of a
//        global varialbe). The contained object is destructed, below.
// =========================================================================

static boost::shared_ptr<A>* holder_ptr = new boost::shared_ptr<A>(
    new A("foo_ptr"));

static boost::shared_ptr<A> get_holder_ptr() {
  return *holder_ptr;
}

static void set_holder_ptr(boost::shared_ptr<A> a_ptr) {
  // Note: I know, it's no good to do that here (quick and dirty):
  Py_AtExit(python_exit);
  // The shared_ptr comes with python::converter::shared_ptr_deleter
  *holder_ptr = a_ptr;
}

void python_exit() {
   std::cout << "\n"
     "Since Python’s internal finalization will have completed before the\n"
     "cleanup function, no Python APIs should be called in or after exit.\n"
     "The boost::python::shared_ptr_deleter will do so, though.\n"
     << std::endl;
   // Destruction but no deallocation.
   holder_ptr->get()->~A();
}


// Fix 3: Put a finalizer object into a module.
// =========================================================================

static boost::shared_ptr<A> holder_finalizer(new A("foo_finalizer"));


struct PythonModuleFinalizer
{
    ~PythonModuleFinalizer();
};

PythonModuleFinalizer::~PythonModuleFinalizer() {
    std::cout << "PythonModuleFinalizer" << std::endl;
    holder_finalizer.reset(
        new A("holder_finalizer without shared_ptr_deleter"));
}

static boost::shared_ptr<A> get_holder_finalizer() {
  return holder_finalizer;
}

static void set_holder_finalizer(boost::shared_ptr<A> a_ptr) {
  // The shared_ptr comes with python::converter::shared_ptr_deleter
  holder_finalizer = a_ptr;
}


// Fix 4: Encapsulate the shared_ptr
// =========================================================================

class B {
    private:
    struct I {
        std::string b;
        I(const std::string& b): b(b) {}
        ~I() { std::cout << "Destruct: " << b << std::endl; }
    };

    public:
    B(std::string b): s(new I(b)) {}
    std::string str() { return s.get()->b; }

    private:
    boost::shared_ptr<I> s;
};

static B holder_encapsulate("foo_encapsulate");


static B get_holder_encapsulate() {
  return holder_encapsulate;
}

static void set_holder_encapsulate(B b) {
  holder_encapsulate = b;
}


BOOST_PYTHON_MODULE(test)
{
  using namespace boost::python;

  class_<A, boost::shared_ptr<A> >("A", init<std::string>())
    .def("__str__", &A::str)
    ;

  def("set_holder", &set_holder);
  def("get_holder", &get_holder);
  def("reset_holder", &reset_holder);

  def("set_holder_ptr", &set_holder_ptr);
  def("get_holder_ptr", &get_holder_ptr);

  object finalizer_class = class_<PythonModuleFinalizer
      ("PythonModuleFinalizer", init<>());
  object finalizer = finalizer_class();
  scope().attr("ModuleFinalizer") = finalizer;
  def("set_holder_finalizer", &set_holder_finalizer);
  def("get_holder_finalizer", &get_holder_finalizer);

  class_<B>("B", init<std::string>())
    .def("__str__", &B::str)
  ;
  def("set_holder_encapsulate", &set_holder_encapsulate);
  def("get_holder_encapsulate", &get_holder_encapsulate);

}

python文件:

import test
print(str(test.get_holder()))
test.set_holder(test.A('bar'))
print(str(test.get_holder()))
test.reset_holder()

print(str(test.get_holder_ptr()))
test.set_holder_ptr(test.A('bar_ptr'))
print(str(test.get_holder_ptr()))

print(str(test.get_holder_finalizer()))
test.set_holder_finalizer(test.A('bar_finalizer'))
print(str(test.get_holder_finalizer()))

print(str(test.get_holder_encapsulate()))
test.set_holder_encapsulate(test.B('bar_encapsulate'))
print(str(test.get_holder_encapsulate()))

测试的输出是:

foo
Destruct: foo
bar
reset
Destruct: bar
foo_ptr
Destruct: foo_ptr
bar_ptr
foo_finalizer
Destruct: foo_finalizer
bar_finalizer
foo_encapsulate
Destruct: foo_encapsulate
bar_encapsulate
PythonModuleFinalizer
Destruct: bar_finalizer

Since Python’s internal finalization will have completed before the
cleanup function, no Python APIs should be called in or after exit.
The boost::python::shared_ptr_deleter will do so, though.

Destruct: bar_ptr
Destruct: bar_encapsulate
Destruct: holder without shared_ptr_deleter