Skip to content

Commit

Permalink
Fix memory leak in sh2peaks
Browse files Browse the repository at this point in the history
The precomputer instance in Processor was allocated as a raw pointer and thus never freed.
  • Loading branch information
daljit46 committed Nov 29, 2023
1 parent 87f3b01 commit 0d27084
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/sh2peaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Processor {
threshold(threshold),
peaks_out(npeaks),
ipeaks_vox(ipeaks_data),
precomputer(use_precomputer ? new Math::SH::PrecomputedAL<value_type>(lmax) : nullptr) {}
precomputer(use_precomputer ? std::make_shared<Math::SH::PrecomputedAL<value_type>>(lmax) : nullptr) {}

bool operator()(const Item &item) {

Expand All @@ -175,7 +175,7 @@ class Processor {

for (size_t i = 0; i < size_t(dirs.rows()); i++) {
Direction p(dirs(i, 0), dirs(i, 1));
p.a = Math::SH::get_peak(item.data, lmax, p.v, precomputer);
p.a = Math::SH::get_peak(item.data, lmax, p.v, precomputer.get());
if (std::isfinite(p.a)) {
for (size_t j = 0; j < all_peaks.size(); j++) {
if (abs(p.v.dot(all_peaks[j].v)) > DOT_THRESHOLD) {
Expand Down Expand Up @@ -249,7 +249,7 @@ class Processor {
value_type threshold;
vector<Direction> peaks_out;
Image<value_type> ipeaks_vox;
Math::SH::PrecomputedAL<value_type> *precomputer;
std::shared_ptr<Math::SH::PrecomputedAL<value_type>> precomputer;

bool check_input(const Item &item) {
if (ipeaks_vox.valid()) {
Expand Down

0 comments on commit 0d27084

Please sign in to comment.