diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 32e245b46..9e9af98d8 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -225,9 +225,9 @@ struct buffer_info { : buffer_info(ptr, itemsize, format, 1, std::vector { size }, std::vector { itemsize }) { } - buffer_info(Py_buffer *view) + buffer_info(Py_buffer *view, bool ownview = true) : ptr(view->buf), itemsize((size_t) view->itemsize), size(1), format(view->format), - ndim((size_t) view->ndim), shape((size_t) view->ndim), strides((size_t) view->ndim), view(view) { + ndim((size_t) view->ndim), shape((size_t) view->ndim), strides((size_t) view->ndim), view(view), ownview(ownview) { for (size_t i = 0; i < (size_t) view->ndim; ++i) { shape[i] = (size_t) view->shape[i]; strides[i] = (size_t) view->strides[i]; @@ -236,11 +236,12 @@ struct buffer_info { } ~buffer_info() { - if (view) { PyBuffer_Release(view); delete view; } + if (view && ownview) { PyBuffer_Release(view); delete view; } } private: Py_buffer *view = nullptr; + bool ownview = false; }; NAMESPACE_BEGIN(detail)