jrenaud90 / CyRK

Runge-Kutta ODE Integrator Implemented in Cython and Numba
Other
12 stars 2 forks source link

Arrays returned by `WrapCySolverResult` can be deleted while refs to arrays are still alive. #80

Closed jrenaud90 closed 16 hours ago

jrenaud90 commented 20 hours ago

As noted in Issue #78, if arrays from WrapCySolverResult that are stored in a local variable are merely references to the underlying memory held by the WrapCySolverResult instance. If that instance is destroyed, then the variables are no longer pointing to valid memory.

Fix is to add a wrapper np.copy around the np.asarray in the following return values of the WrapCySolverResult class.

@property
def t(self):
    return np.asarray(self.time_view, dtype=np.float64, order='C')

@property
def y(self):
    return np.asarray(self.y_view, dtype=np.float64, order='C').reshape((self.size, self.num_dy)).T
jrenaud90 commented 19 hours ago

New tests are showing the issue. working on fix

dihm commented 19 hours ago

An equivalent alternative fix is to replace np.asarray with np.array. Not sure there is any significant benefit from one way or the other.

jrenaud90 commented 16 hours ago

Hey @dihm - I have a fix in v0.12.1 which is on pypi now (via pip) and will be on conda forge soon. It is passing my tests but give it a try and see if it works for you! If not please reopen this issue