From be3285208cae6260ef8dd87f8c0067fbf3b6a370 Mon Sep 17 00:00:00 2001 From: Matthew Scroggs Date: Tue, 17 Sep 2024 15:14:18 +0100 Subject: [PATCH] only __del__ if owned (#36) --- python/ndelement/ciarlet.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/python/ndelement/ciarlet.py b/python/ndelement/ciarlet.py index ec9979b..b83db98 100644 --- a/python/ndelement/ciarlet.py +++ b/python/ndelement/ciarlet.py @@ -6,6 +6,7 @@ from ndelement._ndelementrs import lib as _lib, ffi as _ffi from ndelement.reference_cell import ReferenceCellType, entity_counts, dim from enum import Enum +from _cffi_backend import _CDataBase class Continuity(Enum): @@ -44,13 +45,15 @@ class MapType(Enum): class CiarletElement(object): """Ciarlet element.""" - def __init__(self, rs_element): + def __init__(self, rs_element: _CDataBase, owned: bool = True): """Initialise.""" self._rs_element = rs_element + self._owned = owned def __del__(self): """Delete object.""" - _lib.ciarlet_free_element(self._rs_element) + if self._owned: + _lib.ciarlet_free_element(self._rs_element) @property def dtype(self): @@ -181,13 +184,15 @@ def tabulate(self, points: npt.NDArray[np.floating], nderivs: int) -> npt.NDArra class ElementFamily(object): """Ciarlet element.""" - def __init__(self, rs_family): + def __init__(self, rs_family: _CDataBase, owned: bool = True): """Initialise.""" self._rs_family = rs_family + self._owned = owned def __del__(self): """Delete object.""" - _lib.ciarlet_free_family(self._rs_family) + if self._owned: + _lib.ciarlet_free_family(self._rs_family) def element(self, cell: ReferenceCellType) -> CiarletElement: return CiarletElement(_lib.element_family_element(self._rs_family, cell.value))