shared_array holder to prevent premature garbage collection

Issue #94 on hold
Eric Harper created an issue

It is possible for shared_array (to be shared_ptr) data to be garbage collected at the C++ level before it should be at the python level, e.g.

calculated_data = rdf.getRDF()
# something happens in which rdf passes out of scope, but calculated_data still exists
# has happened in practice with Plato scripts
someFunction(calculated_data)
# this may/will seg_fault

Solution proposed by Josh:

class shared_arrray_holder<T>
    {
    shared_arrray_holder<T> (boost::shared_array<T> _arr) : arr(_arr) {}
    boost::shared_array<T> arr;
    }

...

boost::shared_array<T> data = something.getSomething()
arr_obj = PyArraySimpleFromData(data.get())
holder_obj = shared_array_holder<T>(data)
arr_obj.__dict__['_fred_array_holder'] = holder_obj.

Josh also suggested that this may be able to be done in cython without the need of this c++ code.

Would need to be changed to shared_ptr

Comments (5)

  1. Eric Harper reporter

    Started attempt, but looks like I've run into (perhaps) a brick wall:

    Error compiling Cython file:

    ... cdef shared_ptr[float] particle_op = self.thisptr.getParticleCubaticOrderParameter() cdef np.npy_intp nbins[1] nbins[0] = <np.npy_intp>self.thisptr.getNumParticles() cdef np.ndarray[np.float32_t, ndim=1] result = np.PyArray_SimpleNewFromData(1, nbins, np.NPY_FLOAT32,<void>particle_op.get()) cdef SharedPtrHolder holder_obj = SharedPtrHolder(particle_op) result.dict["_freud_array_holder"] = <void>holder_obj.get() ^


    order.pxi:299:49: Cannot convert 'void ' to Python object cpp/CMakeFiles/_freud.dir/build.make:71: recipe for target '/home/harperic/code/freud/freud/_freud.cpp' failed make[2]: [/home/harperic/code/freud/freud/_freud.cpp] Error 1 make[2]: Deleting file '/home/harperic/code/freud/freud/_freud.cpp' CMakeFiles/Makefile2:135: recipe for target 'cpp/CMakeFiles/_freud.dir/all' failed make[1]: [cpp/CMakeFiles/_freud.dir/all] Error 2 Makefile:127: recipe for target 'all' failed make: * [all] Error 2

  2. Eric Harper reporter

    Keep running into the same problem:

    Error compiling Cython file:

    ... cdef shared_ptr[float] particle_op = self.thisptr.getParticleCubaticOrderParameter() cdef np.npy_intp nbins[1] nbins[0] = <np.npy_intp>self.thisptr.getNumParticles() cdef np.ndarray[np.float32_t, ndim=1] result = np.PyArray_SimpleNewFromData(1, nbins, np.NPY_FLOAT32,<void*>particle_op.get()) cdef handle_holder_obj = ArrayHolder() cdef holder_obj = handle_holder_obj.create(particle_op) ^


    order.pxi:325:62: Cannot convert 'shared_ptr[float]' to Python object

    Even when the class is in the same .pxi file

  3. Eric Harper reporter

    Until there is an actual reproducable issue which causes this to be needed, it will not be pursued (currently can't determine an appropriate unit test, and there's only been one instance where this has occured...).

  4. Log in to comment