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

Probable memory leaks #643

Open
BishopWolf opened this issue Dec 17, 2024 · 1 comment
Open

Probable memory leaks #643

BishopWolf opened this issue Dec 17, 2024 · 1 comment

Comments

@BishopWolf
Copy link

BishopWolf commented Dec 17, 2024

In several parts of the c++ code, a raw pointer is used as the result of certain functions. For example, in GateUniqueVolumeID.cpp you have

G4AffineTransform *GateUniqueVolumeID::GetLocalToWorldTransform(size_t depth) {
 {... irrelevant ...}
  auto &rotation = fVolumeDepthID[depth].fRotation;
  auto &translation = fVolumeDepthID[depth].fTranslation;
  auto t = new G4AffineTransform(rotation, translation);
  return t;
}

This is allocating a new G4AffineTransform object on the heap with new. This can lead to a memory leak if the memory is not properly released.

To fix this issue, you should consider using smart pointers or containers that manage memory for you, such as std::unique_ptr or std::shared_ptr. This will ensure that the memory is released when it is no longer needed.

Here is an example of how you can modify the line to use std::unique_ptr:

std::unique_ptr<G4AffineTransform> GateUniqueVolumeID::GetLocalToWorldTransform(size_t depth) {
  {... irrelevant ...}
  auto &rotation = fVolumeDepthID[depth].fRotation;
  auto &translation = fVolumeDepthID[depth].fTranslation;
  auto t = std::make_unique<G4AffineTransform>(rotation, translation); // Critical change
  return t;
}

Issue generated with Snyk

@BishopWolf
Copy link
Author

BishopWolf commented Dec 17, 2024

Places concerned:
core\opengate_core\opengate_lib\GateSingleParticleSourcePencilBeam.h:l52
core\opengate_core\opengate_lib\GateSingleParticleSourcePencilBeam.h:l54
core\opengate_core\opengate_lib\GateInfo.cpp:l71
core\opengate_core\opengate_lib\GateInfo.cpp:l75
core\opengate_core\opengate_lib\GateUniqueVolumeID.cpp:l105
core\opengate_core\opengate_lib\GateUniqueVolumeID.cpp:l120
core\opengate_core\opengate_lib\GateVolumeVoxelizer.cpp:l20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant