Skip to content

Commit

Permalink
memory leak fix; Py_XDCREF amp_func set to NULL (#2394)
Browse files Browse the repository at this point in the history
* memory leak fix; Py_XDCREF amp_func set to NULL

* added test for amp_func memory fix
  • Loading branch information
hammy4815 committed Feb 15, 2023
1 parent 6abb8b3 commit ab52c22
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions python/meep.i
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,7 @@ meep::volume_list *make_volume_list(const meep::volume &v, int c,

%typemap(freearg) double (*)(const meep::vec &) {
Py_XDECREF(py_callback);
py_callback = NULL;
}

%typecheck(SWIG_TYPECHECK_POINTER) double (*)(const meep::vec &) {
Expand All @@ -738,6 +739,7 @@ meep::volume_list *make_volume_list(const meep::volume &v, int c,

%typemap(freearg) std::complex<double> (*)(const meep::vec &) {
Py_XDECREF(py_amp_func);
py_amp_func = NULL;
}

// Typemap suite for vector3
Expand Down Expand Up @@ -1050,6 +1052,7 @@ void _get_gradient(PyObject *grad, double scalegrad,

%typemap(freearg) std::complex<double> (*)(const meep::vec &) {
Py_XDECREF(py_amp_func);
py_amp_func = NULL;
}

%typecheck(SWIG_TYPECHECK_POINTER) PyObject *min_max_loc {
Expand Down
32 changes: 31 additions & 1 deletion python/tests/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,44 @@

import numpy as np
from meep.geom import Cylinder, Vector3
from meep.source import ContinuousSource, EigenModeSource, GaussianSource
from meep.source import ContinuousSource, EigenModeSource, GaussianSource, Source

import meep as mp

data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "data"))


class TestEigenModeSource(unittest.TestCase):
def test_amp_func_change_sources(self):
src = ContinuousSource(5.0)
center = Vector3()
size = Vector3(0, 1, 0)

ampfunc = lambda X: 1.0
amp_source = [
Source(src, component=mp.Ez, center=center, size=size, amp_func=ampfunc)
]
sim = mp.Simulation(
cell_size=Vector3(1, 1, 0), resolution=5, sources=amp_source
)
sim.run(until=1)

sim.restart_fields()
sim.clear_dft_monitors()
default_lattice = [
EigenModeSource(src, size=size, center=center),
EigenModeSource(src, size=size, center=center),
]
sim.change_sources(default_lattice)
sim.run(until=1)

sim.reset_meep()
sim.change_sources(amp_source)
sim.run(until=1)

self.assertTrue(sim.sources[0].amp_func is ampfunc)
self.assertTrue(sim.sources[0].amp_func is not None)

def test_eig_lattice_defaults(self):
src = ContinuousSource(5.0)
center = Vector3()
Expand Down

0 comments on commit ab52c22

Please sign in to comment.