Fix memory leak in TRestDetectorHitsEvent destructor - #124
Merged
Conversation
|
I tried to reproduce the memory leak reported in the issue rest-for-physics/framework#542 and it is still there with this changes 😭 |
This was referenced Apr 20, 2026
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.
Member
|
I believe with the additions of commit 8d96e7e this is in a good state to be merged. |
Vindaar
approved these changes
Jul 22, 2026
Vindaar
left a comment
Member
There was a problem hiding this comment.
Approved with additional minor changes related to ROOT's ownership semantics. See last commit message for more info.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixed memory leak in
TRestDetectorHitsEventdestructor.Before, only
fHitswas 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