[BUG]: totally different memory, but same id using def_buffer
kaixiongg opened this issue · comments
kai commented
What version (or hash if on master) of pybind11 are you using?
pybind11-2.11.1
Problem description
I created two different arrays in C++ and exposed them to Python using def_buffer. However, I discovered that the id of these two arrays is the same when they have the same size. The id only differs if the sizes of the arrays are different.
Reproducible example code
Bug proof:
>>> id(q1.values)
140509759967120
>>> id(q2.values)
140509759967120
>>> q1
→symbols s_0 s_1 s_2
↓timestamps
0 2 0.2519 0.2516
1 0.4379 0.3865 0.2653
2 0.5595 0.695 0.05404
[3 rows x 3 columns, dtype=FLOAT64]
>>> q2
→symbols s_0 s_1 s_2
↓timestamps
0 0.9205 0.3005 0.9188
1 0.8877 0.8338 0.3876
2 0.6583 0.1229 0.7219
[3 rows x 3 columns, dtype=FLOAT64]
===================================
===================================
The way I use pybind:
.def_property_readonly(
"values",
[](const QTensor& tensor)
{
py::object np = py::module::import("numpy");
auto array = np.attr("array")("object"_a = tensor, "copy"_a = false);
return array;
})
.def_buffer(
[](QTensor& m) -> py::buffer_info
{
return py::buffer_info(m.mutable_data<float>(), sizeof(float), py::format_descriptor<float>::format(), 1, {info->shape_1}, {sizeof(float)});
}
kai commented
I figured it out. When we use property to return something to python, it is jsut a temp address which could be reused next time..