Skip to content

Commit 621f769

Browse files
authored
Merge pull request #167 from zhaozhiwen/fix/135-g4world-volume-leak
Free G4World's G4Volume wrappers; forbid copy/move to avoid double-free
2 parents 6b87e81 + fc25980 commit 621f769

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

gemc/g4system/g4world.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ class G4World : public GBase<G4World>
6363
*/
6464
G4World(const GWorld* gworld, const std::shared_ptr<GOptions>& gopts);
6565

66+
/// Frees the owned G4Volume wrappers. Geant4 owns the contained solid/logical/physical
67+
/// objects, so only the lightweight wrappers allocated by the object factories are deleted.
68+
~G4World() {
69+
for (auto& [name, vol] : g4volumesMap) { delete vol; }
70+
}
71+
72+
// G4World now owns the raw G4Volume* wrappers (freed in the destructor). It is only ever held
73+
// by shared_ptr, so forbid copy/move to avoid a shallow copy double-freeing the wrappers.
74+
G4World(const G4World&) = delete;
75+
G4World& operator=(const G4World&) = delete;
76+
G4World(G4World&&) = delete;
77+
G4World& operator=(G4World&&) = delete;
78+
6679
// ────── lookup / mutators ────────────────────────────────────────
6780

6881
/**

0 commit comments

Comments
 (0)