Skip to content

Commit

Permalink
Fix memory leaks in SWIG wrappers. (#1826)
Browse files Browse the repository at this point in the history
* Fix memory leaks in SWIG wrappers.

* move delete into material_free

Co-authored-by: Steven G. Johnson <stevenj@alum.mit.edu>
  • Loading branch information
kkg4theweb and stevengj authored Nov 19, 2021
1 parent 03ea7ad commit c7948dc
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 7 deletions.
1 change: 0 additions & 1 deletion libpympb/pympb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,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;
Expand Down
5 changes: 2 additions & 3 deletions python/meep.i
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -983,7 +982,6 @@ void _get_gradient(PyObject *grad, double scalegrad, PyObject *fields_a, PyObjec
%typemap(freearg) material_type {
if ($1) {
material_free($1);
delete $1;
}
}

Expand Down Expand Up @@ -1488,6 +1486,7 @@ void _get_gradient(PyObject *grad, double scalegrad, PyObject *fields_a, PyObjec
// 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;
Expand Down Expand Up @@ -1989,7 +1988,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;
Expand Down
1 change: 0 additions & 1 deletion python/typemap_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,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;
Expand Down
2 changes: 1 addition & 1 deletion src/meepgeom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion tests/array-slice-ll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<meep_geom::material_data, decltype(material_deleter)> dielectric(
meep_geom::make_dielectric(eps), material_deleter);
Expand Down

0 comments on commit c7948dc

Please sign in to comment.