From a1e4735743f8b11544a2ef631cd24fe1b69af7df Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 29 Apr 2015 14:38:45 -0400 Subject: [PATCH] [Python] Fix premature deallocation when slicing Solution objects Testing the value of self.parent in _SolutionBase.__dealloc__ was ineffective because this attribute can already be cleared when __dealloc__ is called, causing the borrowed C++ objects to be deleted prematurely. A C data member needs to be used to determine whether this object is responsible for deleting the underlying C++ objects. --- interfaces/cython/cantera/_cantera.pxd | 1 + interfaces/cython/cantera/base.pyx | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 47fb520bb..39c74c638 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -747,6 +747,7 @@ cdef class _SolutionBase: cdef int thermo_basis cdef np.ndarray _selected_species cdef object parent + cdef cbool is_slice cdef class ThermoPhase(_SolutionBase): cdef double _mass_factor(self) diff --git a/interfaces/cython/cantera/base.pyx b/interfaces/cython/cantera/base.pyx index a58dfe080..a40a578bb 100644 --- a/interfaces/cython/cantera/base.pyx +++ b/interfaces/cython/cantera/base.pyx @@ -9,7 +9,8 @@ cdef class _SolutionBase: # keep a reference to the parent to prevent the underlying # C++ objects from being deleted - self.parent = other + self.parent = origin + self.is_slice = True self.thermo = other.thermo self.kinetics = other.kinetics @@ -19,6 +20,8 @@ cdef class _SolutionBase: self._selected_species = other._selected_species.copy() return + self.is_slice = False + if infile or source: self._init_cti_xml(infile, phaseid, phases, source) elif thermo and species: @@ -128,7 +131,7 @@ cdef class _SolutionBase: def __dealloc__(self): # only delete the C++ objects if this is the parent object - if self.parent is None: + if not self.is_slice: del self.thermo del self.kinetics del self.transport