Skip to content

Return the ASCII system stream as unique_ptr to stop a per-load leak#159

Merged
maureeungaro merged 1 commit into
gemc:mainfrom
zhaozhiwen:fix/132-text-stream-leak
Jun 13, 2026
Merged

Return the ASCII system stream as unique_ptr to stop a per-load leak#159
maureeungaro merged 1 commit into
gemc:mainfrom
zhaozhiwen:fix/132-text-stream-leak

Conversation

@zhaozhiwen

Copy link
Copy Markdown
Collaborator

gSystemTextFileStream returned a heap-allocated std::ifstream*; callers only close()d it and never deleted, leaking one std::ifstream per ASCII geometry/material load (and on the not-found / nullptr paths). The leak grows with geometry reloads, so long interactive sessions accumulate it.

Return std::unique_ptr<std::ifstream> so ownership transfers to the caller and the stream is closed and freed on scope exit. The three return IN; sites move the same unique_ptr; return nullptr; constructs an empty one. Callers drop the now-redundant IN->close() (RAII handles it).

Validation: the text factory and both callers compile clean (Geant4 11.4.1 dev container).

Fixes #132

gSystemTextFileStream returned a heap-allocated std::ifstream*; callers
only close()d it and never delete()d, leaking one std::ifstream per ASCII
geometry/material load (and on the not-found / nullptr paths). The leak
grows with geometry reloads, so long interactive sessions accumulate it.

Return std::unique_ptr<std::ifstream> so ownership transfers to the
caller and the stream is closed and freed on scope exit. The three
return IN; sites move the same unique_ptr; return nullptr; constructs an
empty one. Callers drop the now-redundant IN->close() (RAII handles it).

Fixes gemc#132

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@maureeungaro maureeungaro merged commit 6b87e81 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.

[Medium] Stop leaking heap ifstream on every ASCII system/material load

2 participants