From d7adf0ab17e23f8efbb4523a92826e73d1e8b36f Mon Sep 17 00:00:00 2001 From: Bernardo Covas Date: Mon, 13 Oct 2025 23:04:33 +0000 Subject: [PATCH 1/2] [lang] free ndarray memory on the base class classes inheriting from Ndarray should have their memory released --- python/taichi/lang/_ndarray.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/python/taichi/lang/_ndarray.py b/python/taichi/lang/_ndarray.py index 796366adf345e..5e990735671de 100644 --- a/python/taichi/lang/_ndarray.py +++ b/python/taichi/lang/_ndarray.py @@ -60,6 +60,14 @@ def __getitem__(self, key): """ raise NotImplementedError() + def __del__(self): + if impl is None or self.arr is None: + return + + rt = impl.get_runtime() + if rt is not None and rt.prog is not None: + rt.prog.delete_ndarray(self.arr) + @python_scope def fill(self, val): """Fills ndarray with a specific scalar value. @@ -243,9 +251,6 @@ def __init__(self, dtype, arr_shape): self.shape = tuple(self.arr.shape) self.element_type = dtype - def __del__(self): - if impl is not None and impl.get_runtime() is not None and impl.get_runtime().prog is not None: - impl.get_runtime().prog.delete_ndarray(self.arr) @property def element_shape(self): From 4e581320b5f561f6f1711f0b32853b364cf90a18 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 13 Oct 2025 23:39:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- python/taichi/lang/_ndarray.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/taichi/lang/_ndarray.py b/python/taichi/lang/_ndarray.py index 5e990735671de..ed6fee7b863b7 100644 --- a/python/taichi/lang/_ndarray.py +++ b/python/taichi/lang/_ndarray.py @@ -66,7 +66,7 @@ def __del__(self): rt = impl.get_runtime() if rt is not None and rt.prog is not None: - rt.prog.delete_ndarray(self.arr) + rt.prog.delete_ndarray(self.arr) @python_scope def fill(self, val): @@ -251,7 +251,6 @@ def __init__(self, dtype, arr_shape): self.shape = tuple(self.arr.shape) self.element_type = dtype - @property def element_shape(self): return ()