# Delete TFile doesn't work. SysError in \<TFile::TFile\>: file ~~.root can not be opened for reading (Too many open files)

**URL:** <https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429>\
**Category:** ROOT\
**Created:** [June 5, 2019, 5:57am UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429 "2019-06-05T05:57:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 5, 2019, 5:57am UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/1 "2019-06-05T05:57:52Z")

</div>

Hello,  
I built 3 programs that do the following:

1. c++ executable that is opened by bash, reads, performs analysis, writing a TFile then exits.
2. c++ executable that is opened by a bash script that lists all files in specified directories, performs analysis, updates all TFiles (one at a time but loops through all files within the single executable) related then exits.
3. c++ executable that is opened by a bash script that lists all files in specified directories, reads all TFiles related (one at a time but loops through all files within the single executable) then exits.

\break

1. Runs perfectly fine as it dynamically kills the executable after an individual TFile freeing up resources.
2. Originally had “Too many open files” issue, but changing making sure to closing and deleting the TFiles in the loop helped. Now runs well.
3. Declares the following:  
TFile\*  
TObject\*  
2TH1I\*  
1TH2I\*  
TCanvas\*  
In the beginning, I check for null pointer

```auto
    TFile * file=NULL;
   //Check for zombie
    file = new TFile( filename.c_str() , "read");
    if ( file==NULL){//file->IsZombie() ){
        cout<<"This is a dead file"<<endl;
        file->Close();
        delete file;
        return;

```

then at the end of the method, I close  
TCanvas, TFile and delete TCanvas, TFile and TObject I seem to get the Too many open files error.

I am having trouble understanding how to properly “close” files and how TFile counts as a “file” by bash. I am thinking of changing programs 2 and 3 to similar type as 1 to avoid this problem, but is there another solution? Is there a proper way to close TFiles?

As you’ve seen, right now I close TFile\* file by first  
file-\>Close();  
then  
delete file;

Thank you for your time.

// I should mention that I can go around it with setting ulimit to hard limit, but I rather not because this is more TFile issue rather than number of files that need to be open.

* * *

\_ROOT Version: 6+  
_Platform:_ OpenSUSE LEAP 15.0  
_Compiler:_ g++7+

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 5, 2019, 7:02am UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/2 "2019-06-05T07:02:14Z")

</div>

I guess @pcanal can help you.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 5, 2019, 4:50pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/3 "2019-06-05T16:50:05Z")

</div>

I think you meant to write (to avoid using a nullptr):

```auto
    TFile * file = nullptr;
   //Check for zombie
    file = new TFile( filename.c_str() , "read");
    if ( file==nullptr) {
        cout<<"This is a dead file"<<endl;
        return;
    } else if (file->IsZombie()) {
        cout<<"This is a Zombie file"<<endl;
        delete file; // This also does the Close when appropriate.
        return;
    }

```

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 5, 2019, 5:18pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/4 "2019-06-05T17:18:50Z")

</div>

Hello,  
You are right, but it doesn’t help me close and delete TFile properly since after this point, the pointer is still pointing at the file.

My question is to get around the error

# [SysError in \<TFile::TFile\>: file ~~.root can not be opened for reading (Too many open files)](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 5, 2019, 5:24pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/5 "2019-06-05T17:24:33Z")

</div>

```auto
TFile *file = TFile::Open(filename.c_str()); // try to open it for "reading"
if ((!file) || file->IsZombie()) { delete file; return; } // just a precaution
// ...
// ... retrieve and use any objects you need from your "file"
// ...
delete file; // no longer needed ("cleanup")

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 5, 2019, 5:34pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/6 "2019-06-05T17:34:20Z")

</div>

As Wile is pointing out you also need to delete the files that are successfully opened.

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 5, 2019, 5:39pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/7 "2019-06-05T17:39:09Z")

</div>

Thank you @Wile_E_Coyote and @pcanal

I actually read one of the posts suggesting using TFile::option rather than to use the constructor, and unfortunately it didn’t change the outcome for me.

Furthermore, I found that if I use the TFile:: option, I get a segment fault if the file does not exist.

However I will try the following:

1. use nullptr instead of value NULL
2. delete file as soon as the elements I am looking for has been read.

A quick question though. What if iI am opening the file with “update” option to read, then write new components in it?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 5, 2019, 7:09pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/8 "2019-06-05T19:09:10Z")

</div>

> [@SJLPHI](#):
>
> than to use the constructor, and unfortunately it didn’t change the outcome for me.

It would not. For local file they are equivalent.

> get a segment fault if the file does not exist.

Maybe you are not checking for nullptr before using it …

> What if iI am opening the file with “update” option to read, then write new components in it?

Don’t forget to call ‘file-\>Write()’ before deleting it then.

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 6, 2019, 6:05pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/9 "2019-06-06T18:05:13Z")

</div>

Using the null pointer and using the constructor  
TFile file\* = new TFile(file, “update”);  
followed by  
file-\>Close();  
delete file;  
at the end of the loop so far seems to work so far.

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 15, 2019, 6:31am UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/10 "2019-06-15T06:31:56Z")

</div>

It seemed to have solved the issue but I am getting the same error yet again. I am confused and do not understand beause I did it very carefully. All pointers are kept in their original state until delete, and all pointers are deleted, and the only one TFile\* is closed and deleted.

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 15, 2019, 7:51am UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/11 "2019-06-15T07:51:23Z")

</div>

Question from this post:

> [@Too many open files](https://root-forum.cern.ch/t/too-many-open-files/9237):
>
> Dear Rooters, I am writing a macro where I wish to read histograms from 1000 files and add them together. I open each file at the time in a loop using: TFile \*myFile = new TFile(); myFile-\>Open(fileName,“read”); From the file I copy two histograms (I don’t think this is relevant but) using the ROOT feature that whenever a file is opened you automatically enter this file: h\_Ptmp = new TH1F(\*h\_primaryElectronFluenceSpectrum); h\_Stmp = new TH1F(\*h\_scatterElectronFluenceSpectrum); Finally I c…

How did TH1::AddDirectory(kFALSE) help?  
Furthermore,  
TFile \*file = TFile::Open(filename.c\_str());  
yields segment fault whereas  
file = new TFile( filename.c\_str() , “read”);  
does not, if the root file exists. The constructor gives me a chance to check for null pointer before crashing.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 15, 2019, 8:44am UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/12 "2019-06-15T08:44:01Z")

</div>

```cpp
TFile *file = TFile::Open(filename.c_str(), "UPDATE"); // open a file for writing
if ((!file) || file->IsZombie()) { delete file; return; } // just a precaution
// ...
// ... retrieve and / or create and use any objects you need
// ...
file->Write(); // make sure everything is really written
delete file; // no longer needed ("cleanup")

```

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 15, 2019, 10:30am UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/13 "2019-06-15T10:30:31Z")

</div>

Unfortunately, still not working. In fact using the function Tfile::Open crashes the executable way earlier than before when I would use the constructor.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 15, 2019, 11:45am UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/14 "2019-06-15T11:45:13Z")

</div>

I guess you need to attach some minimal version of your source code for inspection.

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 15, 2019, 12:11pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/15 "2019-06-15T12:11:20Z")

</div>

Below is the censored/minimalistic version of the source code.

```auto
#includes...
//Using the C standard namespace
using namespace std;

//Methods shall be listed below until the main.
//many smaller methods censored//
//textdrawer
void drawText(double x, double y, TString tstring, int col, double size=0.04) {
    TLatex tex;// = new TLatex();
    tex.SetNDC();
    tex.SetTextFont(42);
    tex.SetTextSize(size);
    tex.SetTextColor(col);
    tex.DrawLatex(x,y,tstring);
    //delete tex;
}//textdrawer Ends

void load_header(TFile* file, string keywords[], float headerlist[] ){

    TTree *dummytree;
    file->GetObject("file_info", dummytree);
    float dummyfloat=0;
    dummytree->GetEntries();
    for(int i=0; i <17; i++){
        dummytree->SetBranchAddress( keywords[i].c_str() , &dummyfloat);
        dummytree->GetEntry(0);
        headerlist[i]=dummyfloat;
    }
    delete dummytree;
}

//General 1D or 2D plotter Begins
void General_Plotter(string filename, string plot){
    cout<<"Reading "<<filename.c_str()<<endl;
    // TFile * file=nullptr;
   //Check for zombie

    /// TH1::AddDirectory(kFALSE);
    // TH2::AddDirectory(kFALSE);
    // TFile* file = new TFile( filename.c_str() , "read");
    TFile* file = TFile::Open( filename.c_str() , "read");
    if ( file==nullptr){//file->IsZombie() ){
        cout<<"This is a dead file"<<endl;
        file->Close();
        delete file;
        return;
        //exit(0);
    }

    string keywords[17];//censored
    float headerlist[17];
    load_header(file, keywords, headerlist);

    //Reformatting the input filename and plot type
    eliminateExt(filename);
    string outputfilename = newPostfix(filename,".png");
    outputfilename = newPrefix(plot , outputfilename);
    //Outputfilename declared
    string directoryname;
    stringstream dummystringstream;
    dummystringstream.str( std::string() );
    string plotaccessname;
    dummystringstream<<"DIR_"<<plot.c_str();
    cout<<"Plotting "<<plot.c_str()<<filename.c_str()<<" As "<<outputfilename.c_str()<<endl;
    dummystringstream<<extension_count(filename);
    directoryname=dummystringstream.str();
    //Directoryname declared
    cout<<"Creating directory: "<<directoryname.c_str()<<endl;
    dummystringstream.str( std::string() );
    dummystringstream<<"mkdir -p "<<directoryname<<endl;
    string directorycommand=dummystringstream.str();
    //made Directory
    dummystringstream.str( std::string() );
    dummystringstream<<plot.c_str()<<filename.c_str();
    plotaccessname= dummystringstream.str();
    dummystringstream.str( std::string() );
	
	
    TObject *drawer= file->Get( plotaccessname.c_str() ) ;
    //Getting the plot
    //Checking for dead plot
    if( drawer==nullptr ){ // drawer->IsZombie() ){
        cout<<"This is a dead plot"<<endl;
        delete drawer;
        file->Close();
        delete file;
        return;
        //exit(0);
    }
    //Canvas for drawing objects
    TCanvas *can = new TCanvas( plotaccessname.c_str(),plotaccessname.c_str(), 2560,1440);
    system (directorycommand.c_str() );

    string Classname= drawer->ClassName();
    string outputfilename2 = outputfilename;
    dummystringstream.str( std::string() );
    dummystringstream<<directoryname.c_str()<<"/"<<outputfilename.c_str();
    outputfilename= dummystringstream.str();
    dummystringstream.str( std::string() );

    //If case when TH1I
    if (Classname == "TH1I" ){
        cout<<"It's TH1I"<<endl;
        TH1I* dummy1d= (TH1I*)file-> Get( plotaccessname.c_str() ) ;
        can->SetLogy();
        gStyle->SetOptStat(111111);
        //If it is TH1I print all stats and plot it.
        //If it is a zeroed TH1I change the axis by ghost dummy.
        //dummy1d->GetXaxis()->SetRange(dummy1d->FindFirstBinAbove(0) , dummy1d->FindLastBinAbove(0));
        //check(dummy1d->FindFirstBinAbove(0));
        //check(dummy1d->FindLastBinAbove(0));

        if(plot=="Positives_" || plot == "Negatives_"){
            can->SetLogy(0);
        }

        dummy1d->DrawCopy();
		}
    //If case when TH2I
    if (Classname == "TH2I" ){
        cout<<"It's TH2I"<<endl;
        TH2I* dummy2d=(TH2I*)file->Get( plotaccessname.c_str() ) ;
        gStyle->SetOptStat(0);
        //Eliminate the stats box to see more clearly
        //If it is a zeroes image set to low range to see more clearly
        gStyle->SetPalette(1);
        dummy2d->SetMaximum(2500);
        dummy2d->SetMinimum(-250);

    }//If case when Th2I ends

    float increment=0.015;
    for (int i=0; i<16; i++){
        drawText( 0 ,0.05+increment*i, newPostfix (keywords[i],"="),kGreen, increment );
        drawText( increment*4,0.05+increment*i, to_string( (int)headerlist[i] ),kGreen, increment );
    }
    time_t now = time(0);
    string timestamp = ctime(&now);
    drawText( 0 ,0.90, "Time Stamp =",kRed, increment*2 );
    drawText( increment*4*2,0.90, timestamp ,kRed, increment*2 );
    can->Print( outputfilename.c_str() );
    delete drawer;
    file->Close();
    delete file;
    can->Clear();
    can->Close();
    delete can;
}//General 1D or 2D plotter ends.

//Main Begins
int main(int argc,char *argv[]){
    //If no entry don't do anything... for now apparently.
    if (argc==1){}
    //If more than 1 entry, plot it unless "all is called"
    if (argc>1){
        //If asked for all create a temp list and plot them all
        if(argv[1] == ( char* ) "all" ){
            cout<<"asked for all"<<endl;
            system(" ls | grep '\\.root$' > megarootsfilez.SJL.tmp");
            string dummystring;
            ifstream rootfilelist;
            rootfilelist.open("megarootsfilez.SJL.tmp");
            //While reading the temp file list
            while(rootfilelist >> dummystring){
                cout<<dummystring<<endl;
				General_Plotter(dummystring, /*censored*/);

            }//While read Ends.
        }//All case Ends.
    //Else case
        else{
//censored
        }//Else case Ends
    }//If more than 1 entry case Ends.
    return 0;
}//Main Ends

```

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 15, 2019, 12:24pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/16 "2019-06-15T12:24:21Z")

</div>

You can debug your executable (and / or use valgrind):

```auto
`root-config --cxx --cflags` -O0 -g -Wall -Wextra -o YourExecutable YourMain.cxx `root-config --libs`

```

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 15, 2019, 12:37pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/17 "2019-06-15T12:37:25Z")

</div>

Yes, I have already done that and valgrind gave me 100+ handful of definitely lost referring to the ROOT packages. None of which are coming from my source code, but only from root libraries.

At least valgrind says it’s an issue with root 6.14. I doubt 6.16 will be any better.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 15, 2019, 2:53pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/18 "2019-06-15T14:53:42Z")

</div>

> [@SJLPHI](#):
>
> TFile \*file = TFile::Open(filename.c\_str());  
> yields segment fault whereas  
> file = new TFile( filename.c\_str() , “read”);

This is _very_ unusual. For local files the 2 call do essentially the same. When using TFile::Open what is the stack trace at the crash? When using TFile::Open was is the valgrind error reported (don’t forget to use --suppressions=$ROOTSYS/etc/valgrind-root.supp)

---

<div class="post-metadata">

**Author:** ![SJLPHI](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@SJLPHI](https://root-forum.cern.ch/u/SJLPHI)\
**Post date:** [June 15, 2019, 3:22pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/19 "2019-06-15T15:22:40Z")

</div>

I will use the suppression option, I’ve actually not use that for valgrind. I will get back to you in a while with the stack trace because I am currently working on a work-around which seems to work for now.

I think I described it wrong above,

```auto
TFile *file = TFile::Open(filename.c_str(), "read" );
//Yields segment fault IFF root file for filename.c_str() does not exist.
TFile *file = new TFile( filename.c_str() , “read”);
//Yields error throw IF root file for filename.c_str() does not exist.

```

If I use the former, the executable crashes with a segement fault in case the filename does not give a file address. If I use the latter

```auto
if(file==nullptr){
file->Close();
delete file;
return;
}

```

picks it up and ends the method to the next item.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 15, 2019, 3:25pm UTC](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429/20 "2019-06-15T15:25:23Z")

</div>

```auto
if(file==nullptr){
    file->Close();

```

That should always crash … since the value of ‘file’ is a nullptr …

If using _either_ of the method, the proper error handling can be

```auto
if (!file || file->IsZombie()) {
    delete file;
    return;
}

```

[Next page](https://root-forum.cern.ch/t/delete-tfile-doesnt-work-syserror-in-tfile-tfile-file-root-can-not-be-opened-for-reading-too-many-open-files/34429.md?page=2)
