Skip to content

Commit 3385d5e

Browse files
fix(Base/*Source): leaks about local TFile
A few places in Fair*Source allocate TFiles but don't destruct them properly. Fix this.
1 parent 9380ace commit 3385d5e

2 files changed

Lines changed: 14 additions & 24 deletions

File tree

fairroot/base/source/FairFileSource.cxx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ Bool_t FairFileSource::Init()
226226
for (auto fileName : fInputChainList) {
227227
// Temporarily open the input file to extract information which
228228
// is needed to bring the friend trees in the correct order
229-
TFile* inputFile = TFile::Open(fileName);
230-
if (inputFile->IsZombie()) {
229+
std::unique_ptr<TFile> inputFile{TFile::Open(fileName)};
230+
if ((!inputFile) || inputFile->IsZombie()) {
231231
LOG(fatal) << "Error opening the file " << fileName.Data()
232232
<< " which should be added to the input chain or as friend chain";
233233
}
234234

235235
if (fCheckFileLayout) {
236236
// Check if the branchlist is the same as for the first input file.
237-
Bool_t isOk = CompareBranchList(inputFile, chainName);
237+
Bool_t isOk = CompareBranchList(inputFile.get(), chainName);
238238
if (!isOk) {
239239
LOG(fatal) << "Branch structure of the input file " << fRootFile->GetName()
240240
<< " and the file to be added " << fileName.Data() << " are different.";
@@ -246,9 +246,6 @@ Bool_t FairFileSource::Init()
246246
// GetRunIdInfo(inputFile->GetName(), chainName);
247247
// Add the file to the input chain
248248
fInChain->Add(fileName);
249-
250-
// Close the temporarly file
251-
inputFile->Close();
252249
}
253250
}
254251
fNoOfEntries = fInChain->GetEntries();
@@ -349,25 +346,23 @@ void FairFileSource::AddFriendsToChain()
349346
// then already existing friend chain. If this type of friend tree
350347
// does not exist already create a new friend chain and add the file.
351348
Bool_t inputLevelFound = kFALSE;
352-
TFile* inputFile;
353349
for (auto level : fInputLevel) {
354350
inputLevel = level;
355351

356-
inputFile = TFile::Open(fileName);
357-
if (inputFile->IsZombie()) {
352+
std::unique_ptr<TFile> inputFile{TFile::Open(fileName)};
353+
if ((!inputFile) || inputFile->IsZombie()) {
358354
LOG(fatal) << "Error opening the file " << level.Data()
359355
<< " which should be added to the input chain or as friend chain";
360356
}
361357

362358
// Check if the branchlist is already stored in the map. If it is
363359
// already stored add the file to the chain.
364-
Bool_t isOk = CompareBranchList(inputFile, inputLevel);
360+
Bool_t isOk = CompareBranchList(inputFile.get(), inputLevel);
365361
if (isOk) {
366362
inputLevelFound = kTRUE;
367363
inputFile->Close();
368364
continue;
369365
}
370-
inputFile->Close();
371366
}
372367
if (!inputLevelFound) {
373368
inputLevel = Form("FriendTree_%i", friendType);
@@ -500,7 +495,7 @@ void FairFileSource::CheckFriendChains()
500495
void FairFileSource::CreateNewFriendChain(TString inputFile, TString inputLevel)
501496
{
502497
TDirectory::TContext restorecwd{};
503-
TFile* f = TFile::Open(inputFile);
498+
std::unique_ptr<TFile> f{TFile::Open(inputFile)};
504499

505500
TString folderName1 = FairRootManager::GetFolderName();
506501
TString folderName = Form("/%s", folderName1.Data());
@@ -538,8 +533,6 @@ void FairFileSource::CreateNewFriendChain(TString inputFile, TString inputLevel)
538533

539534
TChain* chain = new TChain(inputLevel, folderName);
540535
fFriendTypeList[inputLevel] = chain;
541-
542-
f->Close();
543536
}
544537

545538
Bool_t FairFileSource::ActivateObject(TObject** obj, const char* BrName)

fairroot/base/source/FairMixedSource.cxx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,14 @@ Bool_t FairMixedSource::Init()
269269
for (auto fileName : fInputChainList) {
270270
// Temporarily open the input file to extract information which
271271
// is needed to bring the friend trees in the correct order
272-
TFile* inputFile = TFile::Open(fileName);
273-
if (inputFile->IsZombie()) {
272+
std::unique_ptr<TFile> inputFile{TFile::Open(fileName)};
273+
if ((!inputFile) || inputFile->IsZombie()) {
274274
LOG(fatal) << "Error opening the file " << fileName.Data()
275275
<< " which should be added to the input chain or as friend chain";
276276
}
277277

278278
// Check if the branchlist is the same as for the first input file.
279-
Bool_t isOk = CompareBranchList(inputFile, chainName);
279+
Bool_t isOk = CompareBranchList(inputFile.get(), chainName);
280280
if (!isOk) {
281281
LOG(fatal) << "Branch structure of the input file " << fRootFile->GetName()
282282
<< " and the file to be added " << fileName.Data();
@@ -287,9 +287,6 @@ Bool_t FairMixedSource::Init()
287287
// GetRunIdInfo(inputFile->GetName(), chainName);
288288
// Add the file to the input chain
289289
fBackgroundChain->Add(fileName);
290-
291-
// Close the temporarly file
292-
inputFile->Close();
293290
}
294291
}
295292
fNoOfEntries = fBackgroundChain->GetEntries();
@@ -405,8 +402,8 @@ void FairMixedSource::FillEventHeader(FairEventHeader* feh)
405402

406403
void FairMixedSource::SetSignalFile(TString name, UInt_t identifier)
407404
{
408-
TFile* SignalInFile = TFile::Open(name.Data());
409-
if (SignalInFile->IsZombie()) {
405+
std::unique_ptr<TFile> SignalInFile{TFile::Open(name.Data())};
406+
if ((!SignalInFile) || SignalInFile->IsZombie()) {
410407
LOG(fatal) << "Error opening the Signal file";
411408
} else {
412409
/** Set a signal file of certain type (identifier) if already exist add the file to the chain*/
@@ -455,8 +452,8 @@ void FairMixedSource::AddBackgroundFile(TString name)
455452
if (name.IsNull()) {
456453
LOG(info) << "No background file defined.";
457454
}
458-
TFile* BGFile = TFile::Open(name);
459-
if (BGFile->IsZombie()) {
455+
std::unique_ptr<TFile> BGFile{TFile::Open(name)};
456+
if ((!BGFile) || BGFile->IsZombie()) {
460457
LOG(fatal) << "Error opening the Background file " << name.Data();
461458
} else {
462459
if (fBackgroundChain != 0) {

0 commit comments

Comments
 (0)