Skip to content

Commit

Permalink
fix memory leaks in structure and fields load during checkpointing (N…
Browse files Browse the repository at this point in the history
…anoComp#1872)

* fix memory leaks in structure and fields load during checkpointing

* delete the chi1inv and fields array if it exists and reallocate

* in unit test, set gaussian source cutoff to 0 due to off-by-1 timestep counter bug

* remove cutoff=0 from unit tests

* lazily allocate H only if B is not NULL

* allocate fields array for H in PML region
  • Loading branch information
oskooi authored and Mo Chen committed Feb 16, 2022
1 parent 1ac33b4 commit 238baca
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/fields_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,17 @@ void fields::load_fields_chunk_field(h5file *h5f, bool single_parallel_file,
size_t n = num_f[(chunk_i * NUM_FIELD_COMPONENTS + c) * 2 + d];
realnum **f = field_ptr_getter(chunks[i], c, d);
if (n == 0) {
delete[] * f;
delete[] *f;
*f = NULL;
} else {
if (n != ntot)
meep::abort("grid size mismatch %zd vs %zd in fields::load", n, ntot);
*f = new realnum[ntot];
// here we need to allocate the fields array for H in the PML region
// because of H = B in fields_chunk::alloc_f whereby H is lazily
// allocated in fields_chunk::update_eh during the first timestep
const direction d_c = component_direction(c);
if (!(*f) || (*f && is_magnetic(component(c)) && chunks[i]->s->sigsize[d_c] > 1))
*f = new realnum[ntot];
my_ntot += ntot;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/structure_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ void structure::load(const char *filename, bool single_parallel_file) {
}
else {
if (n != ntot) meep::abort("grid size mismatch %zd vs %zd in structure::load", n, ntot);
chunks[i]->chi1inv[c][d] = new realnum[ntot];
if (!chunks[i]->chi1inv[c][d]) chunks[i]->chi1inv[c][d] = new realnum[ntot];
my_ntot += ntot;
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/dump_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ int test_periodic(double eps(const vec &), int splitting, const char *tmpdir) {
double ttot = 17.0;

grid_volume gv = vol3d(1.5, 0.5, 1.0, a);
structure s1(gv, eps);
structure s(gv, eps, no_pml(), identity(), splitting);

std::string filename_prefix =
Expand Down

0 comments on commit 238baca

Please sign in to comment.