diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 1c304ee39..a70ad1a69 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -891,13 +891,15 @@ cdef class GasTransportData: cdef _assign(self, shared_ptr[CxxTransportData] other) cdef class _SolutionBase: + cdef shared_ptr[CxxThermoPhase] _thermo cdef CxxThermoPhase* thermo + cdef shared_ptr[CxxKinetics] _kinetics cdef CxxKinetics* kinetics + cdef shared_ptr[CxxTransport] _transport cdef CxxTransport* transport 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 f6397b0f9..5c5b24f66 100644 --- a/interfaces/cython/cantera/base.pyx +++ b/interfaces/cython/cantera/base.pyx @@ -10,21 +10,17 @@ cdef class _SolutionBase: if origin is not None: other = <_SolutionBase?>origin - # keep a reference to the parent to prevent the underlying - # C++ objects from being deleted - self.parent = origin - self.is_slice = True - self.thermo = other.thermo self.kinetics = other.kinetics self.transport = other.transport + self._thermo = other._thermo + self._kinetics = other._kinetics + self._transport = other._transport self.thermo_basis = other.thermo_basis 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: @@ -63,6 +59,7 @@ cdef class _SolutionBase: # Thermo if isinstance(self, ThermoPhase): self.thermo = newPhase(deref(phaseNode)) + self._thermo.reset(self.thermo) else: self.thermo = NULL @@ -76,6 +73,7 @@ cdef class _SolutionBase: # adjacent bulk phases for a surface phase v.push_back(phase.thermo) self.kinetics = newKineticsMgr(deref(phaseNode), v) + self._kinetics.reset(self.kinetics) else: self.kinetics = NULL @@ -85,6 +83,7 @@ cdef class _SolutionBase: the model type and a list of Species objects. """ self.thermo = newThermoPhase(stringify(thermo)) + self._thermo.reset(self.thermo) self.thermo.addUndefinedElements() cdef Species S for S in species: @@ -98,6 +97,7 @@ cdef class _SolutionBase: cdef Reaction reaction if isinstance(self, Kinetics): self.kinetics = CxxNewKinetics(stringify(kinetics)) + self._kinetics.reset(self.kinetics) self.kinetics.addPhase(deref(self.thermo)) for phase in phases: self.kinetics.addPhase(deref(phase.thermo)) @@ -131,10 +131,3 @@ cdef class _SolutionBase: def __copy__(self): raise NotImplementedError('Solution object is not copyable') - - def __dealloc__(self): - # only delete the C++ objects if this is the parent object - if not self.is_slice: - del self.thermo - del self.kinetics - del self.transport diff --git a/interfaces/cython/cantera/transport.pyx b/interfaces/cython/cantera/transport.pyx index c490405b5..9f68544c9 100644 --- a/interfaces/cython/cantera/transport.pyx +++ b/interfaces/cython/cantera/transport.pyx @@ -143,6 +143,7 @@ cdef class Transport(_SolutionBase): if not model: model = 'None' self.transport = newTransportMgr(stringify(model), self.thermo) + self._transport.reset(self.transport) super().__init__(*args, **kwargs) property transport_model: @@ -156,9 +157,8 @@ cdef class Transport(_SolutionBase): return pystr(self.transport.transportType()) def __set__(self, model): - cdef CxxTransport* old = self.transport self.transport = newTransportMgr(stringify(model), self.thermo) - del old # only if the new transport manager was successfully created + self._transport.reset(self.transport) property viscosity: """Viscosity [Pa-s].""" @@ -242,6 +242,7 @@ cdef class DustyGasTransport(Transport): # The signature of this function causes warnings for Sphinx documentation def __init__(self, *args, **kwargs): self.transport = newTransportMgr(stringify("DustyGas"), self.thermo) + self._transport.reset(self.transport) super().__init__(*args, **kwargs) property porosity: