Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memory leak fix; Py_XDCREF amp_func set to NULL #2394

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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