[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.
This commit is contained in:
Ray Speth 2015-04-29 14:38:45 -04:00
parent 25c2d6c781
commit a1e4735743
2 changed files with 6 additions and 2 deletions

View file

@ -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)

View file

@ -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