【发布时间】:2013-12-23 03:54:16
【问题描述】:
获取此代码
#include<boost/python>
namespace bp = boost::python;
bp::list py_points; //initial list
other_class* C; // this class have a bp::list attribute called py_list
// ... some code ....
// in this part C.py_list.ptr() is 0x0
other_class->py_list = py_list; // problem here!!
问题在于运算符“=”
在调试器中的object_core.hpp文件中,这是一个boost python核心文件
inline api::object_base& api::object_base::operator=(api::object_base const& rhs)
{
Py_INCREF(rhs.m_ptr);
Py_DECREF(this->m_ptr); // in this line the program fail
this->m_ptr = rhs.m_ptr;
return *this;
}
使用运算符“=”的正确方法是什么
已编辑
问题出在堆栈上,如果指针 other_class->py_list 为 null(或 None,因为未调用类构造函数)程序无法调用函数 Py_DECREF(不存在NULL 指针之前的引用)
问题是修复调用构造函数
other_class* C = new othe_class(); // fixed!!
【问题讨论】:
标签: c++ python boost-python python-extensions