Repository: heitzmann/gdstk (language: python)

Issue:
Clear Cell result arrays after copying to Python lists
While investigating the memory usage of a Python application, I ran across a couple of cases in `cell_object.cpp` where the the result of `Cell::get_*()` methods was not cleared before returning back to Python.

With the changes in this PR, memory usage of the app remains stable, even when calling `cell.get_polygons()`, `cell.get_labels()`, and/or `cell.get_paths()` in a tight loop.


Discussion / hints:
Thanks, @jatoben, it looks like they were correctly cleared only on the error path, not upon success!

Fix patch:
diff --git a/python/cell_object.cpp b/python/cell_object.cpp
index 0feaca985..397404a3b 100644
--- a/python/cell_object.cpp
+++ b/python/cell_object.cpp
@@ -354,7 +354,7 @@ static PyObject* cell_object_get_paths(CellObject* self, PyObject* args, PyObjec
         obj = (RobustPathObject*)PyObject_Init((PyObject*)obj, &robustpath_object_type);
         obj->robustpath = path;
         path->owner = obj;
-        PyList_SET_ITEM(result, i, (PyObject*)obj);
+        PyList_SET_ITEM(result, i + fp_array.count, (PyObject*)obj);
     }
 
     fp_array.clear();


Test patch:
diff --git a/tests/cell_test.py b/tests/cell_test.py
index b91d98bf9..ef0668445 100644
--- a/tests/cell_test.py
+++ b/tests/cell_test.py
@@ -314,12 +314,15 @@ def test_get_paths(tree):
     paths = c3.get_paths()
     assert len(paths) == 12
     assert paths[0].num_paths == 2
+    assert all(p is not None for p in paths)
     paths = c3.get_paths(depth=1)
     assert len(paths) == 6
     assert paths[0].num_paths == 2
+    assert all(p is not None for p in paths)
     paths = c3.get_paths(depth=1, layer=1, datatype=3)
     assert len(paths) == 6
     assert paths[0].num_paths == 1
+    assert all(p is not None for p in paths)
 
 
 def test_get_labels(tree):