STIR icon indicating copy to clipboard operation
STIR copied to clipboard

Fix memory leak from repeated ProjDataInfo::clone() calls

Open NikEfth opened this issue 2 months ago • 1 comments

This PR fixes a memory leak detected by AddressSanitizer in ScatterSimulation::set_template_proj_data_info. The issue was caused by calling arg.clone() multiple times inside dynamic_cast expressions, leading to leaked heap allocations when the cast failed.

The fix: • Calls clone() exactly once • Transfers ownership safely using a smart pointer • Preserves the existing runtime type checks for supported ProjDataInfo types

This change is purely a memory-safety fix and does not alter behaviour or interfaces.

Changes in this pull request

Testing performed

Related issues

Checklist before requesting a review

  • [X] I have performed a self-review of my code
  • [] I have added docstrings/doxygen in line with the guidance in the developer guide
  • [] I have implemented unit tests that cover any new or modified functionality (if applicable)
  • [X] The code builds and runs on my machine
  • [] documentation/release_XXX.md has been updated with any functionality change (if applicable)

Contribution Notes

Please tick the following:

  • [X] The content of this Pull Request (the Contribution) is intentionally submitted for inclusion in STIR (the Work) under the terms and conditions of the Apache-2.0 License.
  • [X] I (or my institution) have signed the STIR Contribution License Agreement (not required for small changes).

NikEfth avatar Dec 23 '25 09:12 NikEfth

Based on CODACY suggestion I simplified a bit the set_template_proj_data_info() to use the shared_ptr

NikEfth avatar Dec 23 '25 09:12 NikEfth