-
Hi Wenzel, I am trying to expose an internal buffer owned by a C++ class as numpy array using the new struct A {
std::vector<int> buffer; // The buffer managed by C++ but we want to expose it as numpy array in python
};
nb::class_<A>(m, "A")
.def(nb::init<>())
.def_property_readonly("buffer", [](A& A){
size_t shape[] = {A.buffer.size()};
return nb::tensor<nb::numpy, int, nb::shape<nb::any>>(A.buffer.data(), 1, shape);
}); // `nb::rv_policy::reference_internal` is applied by default In python, a memory error can be generated with the following: def create():
a = A()
return a.buffer()
buffer = create() I thought the Lines 725 to 727 in f3044cf I am a bit confused as it is mentioned in the README that "it is no longer necessary that bound types are weak-referenceable". Did I miss anything? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
The The |
Beta Was this translation helpful? Give feedback.
The
nb::reference_internal
feature is intended for cases when you are returning a nanobind-bound object (i.e. something created usingnb::class_<>
).The
nb::tensor
provides anowner
parameter specifically for this purpose. Could you try if this works?