You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
memory corruption by ndarray_instance.data assignment (data should be made readonly) (Trac #1275) #1873
It's probably not a bug, but I'd like to ask for clarification. I try to make two arrays share memory. I create big array from small arrays (this involves a copy e.g. by vstack). Create slice/views of the big array corresponding to the small arrays and do a array.data assignment i.e.:
small_array.data = big_array[some_slice].data
This works, both arrays can now modify the same memory, but is not safe as it can lead to memory corruption (see below). My first question: is this considered very very bad practice and if so how to point an ndarray instance to a new memory safely.
In [1]: import numpy as np
In [2]: zeros = np.zeros((2,2))
In [3]: double_zeros = np.vstack((zeros, zeros))
In [4]: double_zeros
Out[4]:
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
In [5]: zeros.data = double_zeros[0:3].data
In [6]: double_double_zeros = np.vstack((double_zeros,double_zeros))
In [7]: double_double_zeros
Out[7]:
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
In [8]: double_zeros.data = double_double_zeros[0:5].data
In [9]: zeros
Out[9]:
array([[ 1.25794259e-312, 9.42758674e-317],
[ 1.58101007e-322, 2.37151510e-322]])
In [10]: zeros[0][0] = 1.1
In [11]: zeros
Out[11]:
array([[ 9.88131292e-324, 3.95252517e-323],
[ 4.94065646e-324, 2.42092166e-322]])
In [12]: zeros[0][0] = 2
In [13]: *** glibc detected *** /usr/bin/python: corrupted double-linked list: 0x000000000117dab0 ***
(this kills the interpreter)
Title changed from memory corruption by ndarray_instance.data assignment to memory corruption by ndarray_instance.data assignment (data should be made readonly) by @mwiebe on 2011-03-25
Activity
thouis commentedon Oct 19, 2012
@rgommers wrote on 2010-07-18
Asking this on the mailing list will probably get you an answer. Here it probably won't any time soon.
thouis commentedon Oct 19, 2012
@mwiebe wrote on 2011-03-25
This looks like a bug to me - data should be a read-only attribute of the array class. This crashes for me in 1.6 beta.
thouis commentedon Oct 19, 2012
Milestone changed to
NumPy 2.0by @mwiebe on 2011-03-25thouis commentedon Oct 19, 2012
Title changed from
memory corruption by ndarray_instance.data assignmenttomemory corruption by ndarray_instance.data assignment (data should be made readonly)by @mwiebe on 2011-03-25charris commentedon Feb 16, 2014
Oh, that is very, very bad ;) And we should not allow it.
BUG: make data attribute of ndarray read-only
jaimefrio commentedon Mar 27, 2016
Fixed by #7275, at least whenever the deprecation is made an actual error.