Skip to content

Free G4World's G4Volume wrappers; forbid copy/move to avoid double-free#167

Merged
maureeungaro merged 1 commit into
gemc:mainfrom
zhaozhiwen:fix/135-g4world-volume-leak
Jun 13, 2026
Merged

Free G4World's G4Volume wrappers; forbid copy/move to avoid double-free#167
maureeungaro merged 1 commit into
gemc:mainfrom
zhaozhiwen:fix/135-g4world-volume-leak

Conversation

@zhaozhiwen

Copy link
Copy Markdown
Collaborator

G4World stored raw G4Volume* wrappers in g4volumesMap but had no destructor, so every g4world.reset() on geometry reload leaked one wrapper per volume — unbounded growth in long interactive sessions (large SoLID-scale geometries reloaded many times).

Add a destructor that deletes the wrappers — Geant4 owns the contained solid/logical/physical objects, so only the lightweight GEMC wrappers are freed. Because G4World previously inherited GBase's defaulted copy/move, adding an owning destructor would make a shallow copy double-free; G4World is only ever held by shared_ptr, so this also deletes copy and move to forbid relocation (closing the same class of bug as #136).

Validation: rebuilt gemc in the Geant4 11.4.1 dev container — the deleted copy/move compile cleanly, confirming nothing copies or moves G4World.

Fixes #135

G4World stored raw G4Volume* wrappers in g4volumesMap but had no
destructor, so every g4world.reset() on geometry reload leaked one
wrapper per volume — unbounded growth in long interactive sessions.

Add a destructor that deletes the wrappers (Geant4 owns the contained
solid/logical/physical objects, so only the lightweight GEMC wrappers are
freed). Because G4World previously inherited GBase's defaulted copy/move,
adding an owning destructor would make a shallow copy double-free; G4World
is only ever held by shared_ptr, so delete copy and move to forbid
relocation. Verified the deletions don't break the build (nothing copies
or moves G4World).

Fixes gemc#135

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@maureeungaro maureeungaro merged commit 621f769 into gemc:main Jun 13, 2026
41 of 43 checks passed
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

Successfully merging this pull request may close these issues.

[Low] Make G4World own its G4Volume wrappers to stop a leak per volume on geometry reload

2 participants