类的Python boost-toPython转换器已忽略第二个转换方法

2024-05-14 21:48:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我下节课(简化了):

class Value
{
    public:

    Value();
    ~Value();

    void setValue(int value);
    void setValue(double value);
}

我有python boost模块:

^{pr2}$

Python脚本从dll库调用,并使用pydpy_classes中的容器。第一次调用dll库时,std_vector_value类型的使用没有任何问题。 当我在可执行文件中重新加载dll库时,我收到下一个警告:

RuntimeWarning: to-Python converter for class
boost::python::detail::container_element<class std::vector<class Value,class std::allocator<class Value> >,
unsigned __int64,class boost::python::detail::final_vector_derived_policies
<class std::vector<class Value,
class std::allocator<class Value> >,0> > 
already registered; second conversion method ignored.
return f(*args, **kwds)

所以,这意味着:

  • 首次加载dll库时,topython转换器正常注册,python脚本可以使用std_vector_value类型。在
  • 当dll库被重新加载(FreeLibraryLoadLibrary函数)时,到python转换器尝试再次注册并检查它是否已注册时,表示它已经注册,但我不能从python使用std_vector_value类型。在

这种情况只出现在容器类中(如果我使用std::vectorstd::map-通常如果我使用vector_indexing_suite或{}),对于类值,这个警告不会出现。在

我做错什么了?在


Tags: 脚本警告类型value容器classallocatordll
1条回答
网友
1楼 · 发布于 2024-05-14 21:48:17

接下来的问题是:boostpython是从一个exe进程加载的,但是是由不同的DLL库加载的。这意味着我有3个执行python脚本的DLL库。 Python boost类型在重新加载DLL库之前没有任何问题(在进程中调用FreeLibrary和LoadLibrary而不重新启动进程)。 Python boost有静态变量register(register是这个变量的名称)存储对Python boost类型的引用。 启动可执行文件时,对类型的引用将添加到静态变量register。 因此,该检查正常工作:

boost::python::type_info infoVectorValue = boost::python::type_id<VectorClass>();
const boost::python::converter::registration* regVectorValue = boost::python::converter::registry::query(infoVectorValue);
if (regVectorValue == NULL || (*regVectorValue).m_to_python == NULL) {

但是当卸载DLL库时,对这些python boost类型的引用留在静态变量register中,它们会导致释放的DLL库地址。这意味着当再次加载库时,类型检查可以毫无问题地获得所需类型的引用(在我的示例中是VectorClass),但是这个引用已经中断了。在

因此,这个问题的解决方案是静态地将boost python库链接到每个DLL库-每个DLL库都有它自己的静态变量register和boost python类型 为每个DLL库创建引用,当卸载DLL库时,register变量将为此DLL销毁。在

相关问题 更多 >

    热门问题