#include #include #include #include #include #include using namespace std; #define PRINT(msg, var) \ cout.flush(); \ cerr.flush(); \ cout << msg << var << endl; class Test { TFile *mFile; TTree *mInfo; public: Test() { PRINT(" ??? ENTERING IN ", __PRETTY_FUNCTION__) // uncomment to check that gROOT still closes mFile //TFile::AddDirectory(false); mFile = new TFile("test.root", "RECREATE"); PRINT(" ??? mFile = ", mFile) int var = 1; mInfo = new TTree("info", "info"); mInfo->Branch("var", &var); mInfo->Fill(); PRINT(" ??? EXITING FROM ", __PRETTY_FUNCTION__) } ~Test() { PRINT(" ??? ENTERING IN ", __PRETTY_FUNCTION__) PRINT(" ??? mFile = ", mFile) if (mFile != nullptr) { PRINT(" ??? mFile->IsOpen() = ", mFile->IsOpen()) } mFile->Write(); delete mFile; PRINT(" ??? EXITING FROM ", __PRETTY_FUNCTION__) } void Print() { mFile->Print(); mInfo->Scan("*"); } }; Test *create(bool stat) { PRINT(" ??? ENTERING IN ", __PRETTY_FUNCTION__) Test *t = nullptr; if (!stat) { PRINT(" ??? ALLOCATING NEW INSTANCE", "") t = new Test(); } else { PRINT(" ??? USING STATIC INSTANCE", "") static Test ts; t = &ts; } PRINT(" ??? EXITING FROM ", __PRETTY_FUNCTION__) return t; } int main(int argc, char *argv[]) { if (argc < 2) return -1; bool stat = atoi(argv[1]); auto t = create(stat); t->Print(); if (!stat) { PRINT(" ??? MANUALLY DELETING INSTANCE", "") delete t; } // uncomment to check that gROOT does not close mFile, but segmentation violation still happens //gROOT->GetListOfFiles()->Clear("nodelete"); gROOT->GetListOfFiles()->Print(); PRINT(" ??? EXITING PROGRAM", "") return 0; }