From 2ac2e7bf73678533bf139ad87e77d4274f876049 Mon Sep 17 00:00:00 2001 From: Robert Smith Date: Tue, 4 Jul 2023 11:29:52 +1000 Subject: [PATCH] tcksift2: Fix check for allocation of memory for weights vector If path to output weights file is erroneous, do not issue a misleading warning message claiming that the issue relates to an inability to allocate memory. Fixes bug introduced in #626. Solves #2668. Replaces #2671. --- src/dwi/tractography/SIFT2/tckfactor.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/dwi/tractography/SIFT2/tckfactor.cpp b/src/dwi/tractography/SIFT2/tckfactor.cpp index 0375cb5cf2..d28bf78309 100644 --- a/src/dwi/tractography/SIFT2/tckfactor.cpp +++ b/src/dwi/tractography/SIFT2/tckfactor.cpp @@ -344,19 +344,22 @@ namespace MR { { if (size_t(coefficients.size()) != contributions.size()) throw Exception ("Cannot output weighting factors if they have not first been estimated!"); + decltype(coefficients) weights; try { - decltype(coefficients) weights (coefficients.size()); - for (SIFT::track_t i = 0; i != num_tracks(); ++i) - weights[i] = (coefficients[i] == min_coeff || !std::isfinite(coefficients[i])) ? - 0.0 : - std::exp (coefficients[i]); - save_vector (weights, path); + weights.resize (coefficients.size()); } catch (...) { WARN ("Unable to assign memory for output factor file: \"" + Path::basename(path) + "\" not created"); + return; } + for (SIFT::track_t i = 0; i != num_tracks(); ++i) + weights[i] = (coefficients[i] == min_coeff || !std::isfinite(coefficients[i])) ? + 0.0 : + std::exp (coefficients[i]); + save_vector (weights, path); } + void TckFactor::output_coefficients (const std::string& path) const { save_vector (coefficients, path);