Skip to content

Fix memory leak in TRestDetectorHitsEvent destructor - #124

Merged
cmargalejo merged 3 commits into
masterfrom
cris-fix-hitsEvent-memory-leak
Jul 24, 2026
Merged

Fix memory leak in TRestDetectorHitsEvent destructor#124
cmargalejo merged 3 commits into
masterfrom
cris-fix-hitsEvent-memory-leak

Conversation

@cmargalejo

@cmargalejo cmargalejo commented Apr 16, 2026

Copy link
Copy Markdown
Member

cmargalejo Ok: 18 Powered by Pull Request Badge

  • Fixed memory leak in TRestDetectorHitsEvent destructor.

  • Before, only fHits was deleted, leaking XZ, YZ and XYZ hits, and their graphs and histograms (fXZHits, fYZHits, fXYZHits, fXYHitGraph, fXZHitGraph, fYZHitGraph, fXYHisto, fXZHisto, fYZHisto, fXHisto, fYHisto, fZHisto)

  • The problem was that these objects were created in Initialize() and during event processing, but were never freed.

Relates to rest-for-physics/framework#542

@rest-for-physics/detectorlib

@AlvaroEzq

Copy link
Copy Markdown

I tried to reproduce the memory leak reported in the issue rest-for-physics/framework#542 and it is still there with this changes 😭

cmargalejo and others added 2 commits July 21, 2026 12:50
Release the transient hit collections, graphs, and histograms owned by
TRestDetectorHitsEvent when the event is destroyed. All member pointers
are initialized to nullptr, so deleting objects that were never allocated
is well-defined and requires no additional null checks.

TGraph instances are not automatically owned by ROOT directories and
therefore remain the responsibility of the event. Histograms differ:
ROOT attaches newly created TH1 and TH2 objects to the current directory
by default. Detach every cached histogram with SetDirectory(nullptr) so
the event has unambiguous ownership and a closing TFile cannot leave
dangling pointers that are later deleted again.

Reset histogram pointers immediately after deleting cached instances to
preserve a valid state if allocation of a replacement fails.

Add a regression test that creates projection histograms with a TMemFile
as the current directory, verifies that they are detached, and closes the
file before the event is destroyed.

Relevant ROOT documentation:

- ROOT Object Ownership manual (https://root.cern.ch/manual/object_ownership/)
  Explains gDirectory ownership and recommends hist->SetDirectory(nullptr) for explicitly managed histograms.

- TH1 class reference (https://root.cern.ch/doc/master/classTH1.html)
  Documents automatic directory registration, SetDirectory(), and TH1::AddDirectory().

- TGraph class reference (https://root.cern.ch/doc/v626/classTGraph.html)
  Notes that, unlike histograms and trees, TGraph objects are not automatically attached to the current TFile.

- TPad class reference (https://root.cern.ch/doc/master/classTPad.html)
  Explains that ordinary Draw() operations generally store references to the original objects rather than transferring ownership.
@Vindaar

Vindaar commented Jul 22, 2026

Copy link
Copy Markdown
Member

I believe with the additions of commit 8d96e7e this is in a good state to be merged.

@Vindaar Vindaar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with additional minor changes related to ROOT's ownership semantics. See last commit message for more info.

@cmargalejo
cmargalejo merged commit fe2fb2c into master Jul 24, 2026
64 checks passed
@cmargalejo
cmargalejo deleted the cris-fix-hitsEvent-memory-leak branch July 24, 2026 13:45
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.

3 participants