From fb5748b4674bb9ba8f7653d589abda2c5bd78c57 Mon Sep 17 00:00:00 2001 From: Christian Tacke <58549698+ChristianTackeGSI@users.noreply.github.com> Date: Tue, 5 Mar 2024 00:54:12 +0100 Subject: [PATCH] fix: Do not leak TFile in Fair{File,Mixed}Source And refactor into FairFileSourceBase. --- fairroot/base/source/FairFileSource.cxx | 18 +++++++++--------- fairroot/base/source/FairFileSource.h | 7 ++----- fairroot/base/source/FairFileSourceBase.h | 11 ++++++++--- fairroot/base/source/FairMixedSource.cxx | 17 +++++++++-------- fairroot/base/source/FairMixedSource.h | 5 +---- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/fairroot/base/source/FairFileSource.cxx b/fairroot/base/source/FairFileSource.cxx index c2411be931..fe2530030e 100644 --- a/fairroot/base/source/FairFileSource.cxx +++ b/fairroot/base/source/FairFileSource.cxx @@ -42,10 +42,12 @@ #include #include +using fairroot::detail::maybe_owning_ptr; +using fairroot::detail::non_owning; + FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t) : FairFileSourceBase() , fInputTitle(Title) - , fRootFile(f) , fFriendFileList() , fInputChainList() , fFriendTypeList() @@ -73,6 +75,7 @@ FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t) , fEventMeanTime(0.) , fCheckFileLayout(kTRUE) { + fRootFile = maybe_owning_ptr{f, non_owning}; if ((!fRootFile) || fRootFile->IsZombie()) { LOG(fatal) << "Error opening the Input file"; } @@ -82,7 +85,6 @@ FairFileSource::FairFileSource(TFile* f, const char* Title, UInt_t) FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, UInt_t) : FairFileSourceBase() , fInputTitle(Title) - , fRootFile(0) , fFriendFileList() , fInputChainList() , fFriendTypeList() @@ -110,7 +112,7 @@ FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, U , fEventMeanTime(0.) , fCheckFileLayout(kTRUE) { - fRootFile = TFile::Open(RootFileName->Data()); + fRootFile = std::unique_ptr{TFile::Open(RootFileName->Data())}; if ((!fRootFile) || fRootFile->IsZombie()) { LOG(fatal) << "Error opening the Input file"; } @@ -120,7 +122,6 @@ FairFileSource::FairFileSource(const TString* RootFileName, const char* Title, U FairFileSource::FairFileSource(const TString RootFileName, const char* Title, UInt_t) : FairFileSourceBase() , fInputTitle(Title) - , fRootFile(0) , fFriendFileList() , fInputChainList() , fFriendTypeList() @@ -148,7 +149,7 @@ FairFileSource::FairFileSource(const TString RootFileName, const char* Title, UI , fEventMeanTime(0.) , fCheckFileLayout(kTRUE) { - fRootFile = TFile::Open(RootFileName.Data()); + fRootFile = std::unique_ptr{TFile::Open(RootFileName.Data())}; if ((!fRootFile) || fRootFile->IsZombie()) { LOG(fatal) << "Error opening the Input file"; } @@ -294,9 +295,8 @@ Bool_t FairFileSource::Init() void FairFileSource::SetInTree(TTree* tempTree) { - fInTree = nullptr; fInTree = tempTree; - fRootFile = tempTree->GetCurrentFile(); + fRootFile = maybe_owning_ptr{tempTree->GetCurrentFile(), non_owning}; fInChain->Reset(); IsInitialized = kFALSE; Init(); @@ -581,8 +581,8 @@ Bool_t FairFileSource::ActivateObjectAny(void** obj, const std::type_info& info, void FairFileSource::SetInputFile(TString name) { - fRootFile = TFile::Open(name.Data()); - if (fRootFile->IsZombie()) { + fRootFile = std::unique_ptr{TFile::Open(name.Data())}; + if ((!fRootFile) || fRootFile->IsZombie()) { LOG(fatal) << "Error opening the Input file"; } LOG(info) << "FairFileSource set------------"; diff --git a/fairroot/base/source/FairFileSource.h b/fairroot/base/source/FairFileSource.h index 9ac4853c24..be260edfa8 100644 --- a/fairroot/base/source/FairFileSource.h +++ b/fairroot/base/source/FairFileSource.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -52,9 +52,9 @@ class FairFileSource : public FairFileSourceBase void ReadBranchEvent(const char* BrName) override; /**Read specific tree entry on one branch**/ void ReadBranchEvent(const char* BrName, Int_t Entry) override; + void FillEventHeader(FairEventHeader* feh) override; - const TFile* GetRootFile() { return fRootFile; } /** Add a friend file (input) by name)*/ void AddFriend(TString FileName); /**Add ROOT file to input, the file will be chained to already added files*/ @@ -65,7 +65,6 @@ class FairFileSource : public FairFileSourceBase void CreateNewFriendChain(TString inputFile, TString inputLevel); TTree* GetInTree() { return fInChain->GetTree(); } TChain* GetInChain() { return fInChain; } - TFile* GetInFile() { return fRootFile; } void CloseInFile() { if (fRootFile) { @@ -112,8 +111,6 @@ class FairFileSource : public FairFileSourceBase private: /** Title of input source, could be input, background or signal*/ TString fInputTitle; - /**ROOT file*/ - TFile* fRootFile; /** List of all files added with AddFriend */ std::list fFriendFileList; //! std::list fInputChainList; //! diff --git a/fairroot/base/source/FairFileSourceBase.h b/fairroot/base/source/FairFileSourceBase.h index 52fe2f176b..a7ba3d7668 100644 --- a/fairroot/base/source/FairFileSourceBase.h +++ b/fairroot/base/source/FairFileSourceBase.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2022-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2022-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -9,6 +9,7 @@ #ifndef __FAIRROOT__FairFileSourceBase__ #define __FAIRROOT__FairFileSourceBase__ +#include "FairMemory.h" #include "FairSource.h" #include @@ -33,18 +34,22 @@ class FairFileSourceBase : public FairSource Bool_t InitUnpackers() override { return kTRUE; } Bool_t ReInitUnpackers() override { return kTRUE; } + TFile* GetInFile() { return fRootFile.get(); } + const TFile* GetRootFile() { return fRootFile.get(); } + TObjArray* GetListOfFolders() { return &fListFolder; } Bool_t CompareBranchList(TFile* fileHandle, TString inputLevel); protected: - FairFileSourceBase() - : FairSource(){}; + FairFileSourceBase() = default; std::map> fCheckInputBranches{}; //! static bool ActivateObjectAnyImpl(TTree* source, void** obj, const std::type_info& info, const char* brname); private: + /**ROOT file*/ + fairroot::detail::maybe_owning_ptr fRootFile; //! /** list of folders from all input (and friends) files*/ TObjArray fListFolder{16}; //! diff --git a/fairroot/base/source/FairMixedSource.cxx b/fairroot/base/source/FairMixedSource.cxx index 99e8a00e15..6fbfe13a37 100644 --- a/fairroot/base/source/FairMixedSource.cxx +++ b/fairroot/base/source/FairMixedSource.cxx @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -31,11 +31,13 @@ #include // floor, fmod #include +using fairroot::detail::maybe_owning_ptr; +using fairroot::detail::non_owning; + FairMixedSource::FairMixedSource(TFile* f, const char* Title, UInt_t) : FairFileSourceBase() , fRootManager(0) , fInputTitle(Title) - , fRootFile(f) , fFriendFileList() , fInputChainList() , fFriendTypeList() @@ -74,6 +76,7 @@ FairMixedSource::FairMixedSource(TFile* f, const char* Title, UInt_t) , fRunIdFromSG(kFALSE) , fRunIdFromSG_identifier(0) { + fRootFile = maybe_owning_ptr{f, non_owning}; if ((!fRootFile) || fRootFile->IsZombie()) { LOG(fatal) << "Error opening the Input file"; } @@ -86,7 +89,6 @@ FairMixedSource::FairMixedSource(const TString* RootFileName, const char* Title, : FairFileSourceBase() , fRootManager(0) , fInputTitle(Title) - , fRootFile(0) , fFriendFileList() , fInputChainList() , fFriendTypeList() @@ -125,7 +127,7 @@ FairMixedSource::FairMixedSource(const TString* RootFileName, const char* Title, , fRunIdFromSG(kFALSE) , fRunIdFromSG_identifier(0) { - fRootFile = TFile::Open(RootFileName->Data()); + fRootFile = std::unique_ptr{TFile::Open(RootFileName->Data())}; if ((!fRootFile) || fRootFile->IsZombie()) { LOG(fatal) << "Error opening the Input file"; } @@ -137,7 +139,6 @@ FairMixedSource::FairMixedSource(const TString RootFileName, const Int_t signalI : FairFileSourceBase() , fRootManager(0) , fInputTitle(Title) - , fRootFile(0) , fFriendFileList() , fInputChainList() , fFriendTypeList() @@ -176,7 +177,7 @@ FairMixedSource::FairMixedSource(const TString RootFileName, const Int_t signalI , fRunIdFromSG(kFALSE) , fRunIdFromSG_identifier(0) { - fRootFile = TFile::Open(RootFileName.Data()); + fRootFile = std::unique_ptr{TFile::Open(RootFileName.Data())}; if ((!fRootFile) || fRootFile->IsZombie()) { LOG(fatal) << "Error opening the Input file"; } @@ -456,8 +457,8 @@ void FairMixedSource::SetBackgroundFile(TString name) if (name.IsNull()) { LOG(info) << "No background file defined."; } - fRootFile = TFile::Open(name); - if (fRootFile->IsZombie()) { + fRootFile = std::unique_ptr{TFile::Open(name)}; + if ((!fRootFile) || fRootFile->IsZombie()) { LOG(fatal) << "Error opening the Background file " << name.Data(); } } diff --git a/fairroot/base/source/FairMixedSource.h b/fairroot/base/source/FairMixedSource.h index c207e70ec7..7324702ff8 100644 --- a/fairroot/base/source/FairMixedSource.h +++ b/fairroot/base/source/FairMixedSource.h @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (C) 2014-2023 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * Copyright (C) 2014-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * @@ -58,7 +58,6 @@ class FairMixedSource : public FairFileSourceBase void FillEventHeader(FairEventHeader* feh) override; - const TFile* GetRootFile() { return fRootFile; } /** Add a friend file (input) by name)*/ Bool_t ActivateObject(TObject** obj, const char* BrName) override; @@ -132,8 +131,6 @@ class FairMixedSource : public FairFileSourceBase /** Title of input source, could be input, background or signal*/ TString fInputTitle; - /**ROOT file*/ - TFile* fRootFile; /** List of all files added with AddFriend */ std::list fFriendFileList; //! std::list fInputChainList; //!