简体   繁体   English

pybind11 bad_alloc问题与std :: vector <std::shared_ptr<A> &gt;

[英]pybind11 bad_alloc problem with std::vector<std::shared_ptr<A>>

I have structs like this: 我有这样的结构:

struct A{
  void do_stuff(){...does stuff};
}
struct B{
  std::vector<std::shared_ptr<A>> objs;
}

my pybind: 我的pybind:

py::class_<A>(m, "A")
  .def("do_stuff", &A::do_stuff)
py::class_<B>(m, "B")
  .def_readwrite("objs", &B::objs);

Behaviour I'm trying to reach: 我尝试达到的行为:

import mybind

b_instance = mybind.B()

for a_instance in b_instance:
  a_instance.do_stuff()

Unfortunately, I get 不幸的是,我得到了

MemoryError: std::bad_alloc MemoryError:std :: bad_alloc

Please, help me out. 请帮帮我。 Code above is not actually the code I work with. 上面的代码实际上不是我使用的代码。 Class A works by itself, but doesn't when iterating through B. If this code is not enough please write in the comments. 类A本身起作用,但在遍历B时无效。如果此代码不够用,请在注释中写。 Thanks! 谢谢!

I found out what has been wrong. 我发现出了什么问题。 It's more complicated than I posted, because I use overload, and I invoke virtual method from parent class that invokes child virtual method - pybind11 couldn't handle this behaviour. 它比我发布的还要复杂,因为我使用重载,并且我从调用子虚拟方法的父类中调用虚拟方法-pybind11无法处理此行为。 I have it fixed. 我修好了。 If you have mistakes around PYBIND11_OVERLOAD with complex class structure - write a comment or mail me - I believe I can help 如果您在具有复杂类结构的PYBIND11_OVERLOAD周围遇到错误-请写评论或给我发邮件-相信我可以帮您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pybind11和std :: vector —如何使用胶囊释放数据? - Pybind11 and std::vector — How to free data using capsules? 无法从pybind11中的静态函数返回shared_ptr - unable to return shared_ptr from static function in pybind11 python嵌入到C ++中,函数返回shared_ptr(pybind11 / boost_python) - python embedding in C++ with functions returning shared_ptr(pybind11/boost_python) 在SWIG中为Python包装boost :: shared_ptr的std :: vector - Wrapping std::vector of boost::shared_ptr in SWIG for Python 通过使用 pybind11 通过虚函数通过引用传递 std::vector 的问题 - Problems passing a std::vector by reference though virtual functions using pybind11 pybind11 - 如果访问了结构的任何成员,则 boost::optional 结构的 std::vector 成员将被清空 - pybind11 - std::vector member of boost::optional struct is emptied if any members of the struct are accessed 如何使用 pybind11 将堆分配指针的“std::vector”传递给 python? - How to pass a `std::vector` of heap allocated pointers to python using pybind11? pybind11 STL 自动转换器中断 std::list 指针 - pybind11 STL autoconverter breaks std::list pointers 如何使用pybind11将python函数转换为std :: function - How to cast a python function to std::function with pybind11 pybind11:无法让 std::list 在 Python 中工作 - pybind11: Can't get std::list to work in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM