Skip to content

Fix crash when calling buffer.request multiple times #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cmake_install.cmake
.DS_Store
/example/example.so
/example/example.pyd
/build/*
*.sln
*.sdf
*.opensdf
Expand Down
23 changes: 12 additions & 11 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,20 +423,21 @@ class buffer : public object {
buffer_info request(bool writable = false) {
int flags = PyBUF_STRIDES | PyBUF_FORMAT;
if (writable) flags |= PyBUF_WRITABLE;
view = new Py_buffer();
if (PyObject_GetBuffer(m_ptr, view, flags) != 0)
Py_buffer view;
if (PyObject_GetBuffer(m_ptr, &view, flags) != 0)
throw error_already_set();
std::vector<size_t> shape(view->ndim), strides(view->ndim);
for (int i=0; i<view->ndim; ++i) {
shape[i] = (size_t) view->shape[i];
strides[i] = (size_t) view->strides[i];
std::vector<size_t> shape(view.ndim), strides(view.ndim);
for (int i = 0; i < view.ndim; ++i) {
shape[i] = (size_t)view.shape[i];
strides[i] = (size_t)view.strides[i];
}
return buffer_info(view->buf, view->itemsize, view->format,
view->ndim, shape, strides);
buffer_info result(view.buf, view.itemsize, view.format,
view.ndim, shape, strides);

PyBuffer_Release(&view);

return result;
}
~buffer() { if (view) { PyBuffer_Release(view); delete view; } }
private:
Py_buffer *view = nullptr;
};

NAMESPACE_BEGIN(detail)
Expand Down