From ee916fdfab27e5e5b3c6d583955bf67faddefd98 Mon Sep 17 00:00:00 2001 From: Krishna Gadepalli Date: Thu, 11 Nov 2021 18:11:23 -0800 Subject: [PATCH 1/2] Fix memory leaks in SWIG wrappers. --- python/meep.i | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/meep.i b/python/meep.i index 59ac569d7..676ec2351 100644 --- a/python/meep.i +++ b/python/meep.i @@ -1405,6 +1405,7 @@ void _get_gradient(PyObject *grad, PyObject *fields_a, PyObject *fields_f, PyObj if ($1.num_items != 0) { for (int i = 0; i < $1.num_items; i++) { material_free($1.items[i]); + delete $1.items[i]; } } delete[] $1.items; @@ -1511,6 +1512,7 @@ void _get_gradient(PyObject *grad, PyObject *fields_a, PyObject *fields_f, PyObj // it gets garbage collected and the file gets closed. %newobject meep::fields::open_h5file; +%newobject meep::make_output_directory; %newobject _get_eigenmode; %rename(_vec) meep::vec::vec; From 3170beb2ed4a94c176b77187fa5c023f4b0656b0 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Tue, 16 Nov 2021 22:01:07 -0500 Subject: [PATCH 2/2] move delete into material_free --- libpympb/pympb.cpp | 1 - python/meep.i | 5 +---- python/typemap_utils.cpp | 1 - src/meepgeom.cpp | 2 +- tests/array-slice-ll.cpp | 1 - 5 files changed, 2 insertions(+), 8 deletions(-) diff --git a/libpympb/pympb.cpp b/libpympb/pympb.cpp index 0344f7b85..6fe46cc03 100644 --- a/libpympb/pympb.cpp +++ b/libpympb/pympb.cpp @@ -1630,7 +1630,6 @@ void mode_solver::clear_geometry_list() { if (geometry_list.num_items && geometry_list.items) { for(int i = 0; i < geometry_list.num_items; ++i) { material_free((meep_geom::material_data *)geometry_list.items[i].material); - delete (meep_geom::material_data *)geometry_list.items[i].material; geometric_object_destroy(geometry_list.items[i]); } delete[] geometry_list.items; diff --git a/python/meep.i b/python/meep.i index 676ec2351..73edfd8f2 100644 --- a/python/meep.i +++ b/python/meep.i @@ -767,7 +767,6 @@ meep::volume_list *make_volume_list(const meep::volume &v, int c, %typemap(freearg) GEOMETRIC_OBJECT { if ($1.material) { material_free((material_data *)$1.material); - delete (material_data *)$1.material; geometric_object_destroy($1); } } @@ -1001,7 +1000,6 @@ void _get_gradient(PyObject *grad, PyObject *fields_a, PyObject *fields_f, PyObj %typemap(freearg) material_type { if ($1) { material_free($1); - delete $1; } } @@ -1405,7 +1403,6 @@ void _get_gradient(PyObject *grad, PyObject *fields_a, PyObject *fields_f, PyObj if ($1.num_items != 0) { for (int i = 0; i < $1.num_items; i++) { material_free($1.items[i]); - delete $1.items[i]; } } delete[] $1.items; @@ -2014,7 +2011,7 @@ meep_geom::geom_epsilon* _set_materials(meep::structure * s, meep_geom::geom_epsilon *existing_geps, bool output_chunk_costs, const meep::binary_partition *my_bp) { - + meep_geom::geom_epsilon *geps; if (existing_geps) { geps = existing_geps; diff --git a/python/typemap_utils.cpp b/python/typemap_utils.cpp index 0602a72ff..f24bcd568 100644 --- a/python/typemap_utils.cpp +++ b/python/typemap_utils.cpp @@ -1123,7 +1123,6 @@ void gobj_list_freearg(geometric_object_list* objs) { SWIG_PYTHON_THREAD_SCOPED_BLOCK; for(int i = 0; i < objs->num_items; ++i) { material_free((material_data *)objs->items[i].material); - delete (material_data *)objs->items[i].material; geometric_object_destroy(objs->items[i]); } delete[] objs->items; diff --git a/src/meepgeom.cpp b/src/meepgeom.cpp index f273ac55e..4c3da7c22 100644 --- a/src/meepgeom.cpp +++ b/src/meepgeom.cpp @@ -33,7 +33,6 @@ static void set_default_material(material_type _default_material) { if (default_material != NULL) { if (default_material == _default_material) return; material_free((material_type)default_material); - delete (material_type)default_material; default_material = NULL; } @@ -118,6 +117,7 @@ void material_free(material_type m) { delete[] m->weights; m->weights = NULL; + delete m; } bool material_type_equal(const material_type m1, const material_type m2) { diff --git a/tests/array-slice-ll.cpp b/tests/array-slice-ll.cpp index df7c55855..af754384b 100644 --- a/tests/array-slice-ll.cpp +++ b/tests/array-slice-ll.cpp @@ -139,7 +139,6 @@ int main(int argc, char *argv[]) { meep_geom::material_type vacuum = meep_geom::vacuum; auto material_deleter = [](meep_geom::material_data *m) { meep_geom::material_free(m); - delete m; }; std::unique_ptr dielectric( meep_geom::make_dielectric(eps), material_deleter);