Skip to content

Modify shared_ptr to PyDPStep to allow proper cleanup

Bas van der Tol requested to merge fix-pydpstep-ownership into master

The PyDPStep needs to hold a reference to the underlying python implementation to prevent the python object to be garbage collected. This however creates a circular reference because the python object points back to the C++ object. The circular reference prevents to use_count of the shared_ptr to the PyDPStep object to go to zero when it is no longer used on the C++ side.

This MR breaks the cycle by using two smart pointers to the same object: 1) a shared_ptr on the C++ side that does not delete the object but only lets go of the python reference, 2) the python side uses a unique_ptr that will be deleted when the python reference count goes to zero.

The tPyDPStep test has been extended to verify that the smart pointers work as intended by monitoring whether the nextSteps are destroyed at the right time. That is the nextSteps are alive as long a the parent PyDPStep object is alive, and destroyed once the parent PyDPstep is destroyed.

Merge request reports

Loading