10000 Fix locked Java buffer copy for array conversions · scijava/scyjava@a62ec75 · GitHub
[go: up one dir, main page]

Skip to content

Commit a62ec75

Browse files
committed
Fix locked Java buffer copy for array conversions
`memoryview` of Java arrays (jarr) exposes the jarr's buffer protocol and copies the buffer into a page which then is referenced locked by `memoryview` and things that are created from it (e.g. numpy.frombuffer). Changing the underlying jarr's memory and requesting a new `memoryview` does not return an updated view with the jarr's data change as we expect, but instead the initial `memoryview` data state. This is bad. In order to get `memoryview` to reflect the correct jarr data changes you need to dereference (e.g. del or set to None) the `memoryview` and objects that depend on it (e.g. a numpy array created from the view). This patch resolves this issue by obtaining the buffer that `memoryview` is getting and wrapping the buffer with numpy as we want. We can then safely release the buffer (preserving memory) and return a copy to the user. If _jarray_to_ndarray is called again on the same Java array a new buffer is obtained, wrapped with numpy and released. The end result is an updated view of the Java array upon request.
1 parent ce6c8aa commit a62ec75

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/scyjava/_convert.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ def _jarray_to_ndarray(jarr):
618618
}
619619
# fmt: on
620620
dtype = jarraytype_map[element_type]
621-
ndarray = np.frombuffer(memoryview(jarr), dtype=dtype)
621+
bb = bytes(jarr)
622+
ndarray = np.frombuffer(bb, dtype=dtype)
623+
del bb # release the buffer
622624
return ndarray.reshape(_jarray_shape(jarr))
623625

624626

0 commit comments

Comments
 (0)
0