From 81b8845b8e56d6264901c2a6229354e3467d0556 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Fri, 31 May 2013 15:55:53 +0000 Subject: [PATCH] [Cython] Fix memory leaks in Wall and ReactorNet The underlying C++ objects were not being deleted in the destructors for the Python objects. Moving them to be non-pointer members of the Python wrapper classes fixes this problem. --- interfaces/cython/cantera/_cantera.pxd | 2 +- interfaces/cython/cantera/reactor.pyx | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 14458f744..464005549 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -593,7 +593,7 @@ cdef class WallSurface: cdef Kinetics _kinetics cdef class Wall: - cdef CxxWall* wall + cdef CxxWall wall cdef WallSurface left_surface cdef WallSurface right_surface cdef object _velocity_func diff --git a/interfaces/cython/cantera/reactor.pyx b/interfaces/cython/cantera/reactor.pyx index e4b57cf95..aa8b5ed64 100644 --- a/interfaces/cython/cantera/reactor.pyx +++ b/interfaces/cython/cantera/reactor.pyx @@ -258,7 +258,7 @@ cdef class WallSurface: """ def __cinit__(self, Wall wall, int side): self.wall = wall - self.cxxwall = wall.wall + self.cxxwall = &wall.wall self.side = side self._kinetics = None @@ -330,11 +330,6 @@ cdef class Wall: temperature of the reactor it faces. """ - def __cinit__(self, *args, **kwargs): - self.wall = new CxxWall() - self.left_surface = WallSurface(self, 0) - self.right_surface = WallSurface(self, 1) - def __init__(self, left, right, *, name=None, A=None, K=None, U=None, Q=None, velocity=None, kinetics=(None,None)): """ @@ -365,6 +360,9 @@ cdef class Wall: chemistry occurs on only one side, enter ``None`` for the non-reactive side. """ + self.left_surface = WallSurface(self, 0) + self.right_surface = WallSurface(self, 1) + self._velocity_func = None self._heat_flux_func = None @@ -700,12 +698,9 @@ cdef class ReactorNet: >>> reactor_network = ReactorNet([r1, r2]) >>> reactor_network.advance(time) """ - cdef CxxReactorNet* net + cdef CxxReactorNet net cdef list _reactors - def __cinit__(self, *args, **kwargs): - self.net = new CxxReactorNet() - def __init__(self, reactors=()): self._reactors = [] # prevents premature garbage collection for R in reactors: