Segfault with objects clearly there, TFile not closing

I have made a root file of about 860 MB that contains many TH1s, TH2s, and THnSparse. I want to extract these THnSparse in another program I made in order to add them with other THnSparse I’ve made other places.

I only noticed an issue when I started running into segfaults part way through my 145 indexed loop (one loop (i) of 29 and a nested loop (j) of 5). I checked the original file I was reading from to make sure the THnSparse I was trying to pull from was there, and it was.

Upon further inspection, I found that this root file was having some problems when being read in. It said

Warning in <TFile::Init>: <FILENAME> probably not closed, trying to recover

and then would go through every TH1, TH2, and THnSparse present in the file saying that it recovered them at a given address.

I’ve looked at some other posts regarding issues with files not closing, but didn’t find anything that seemed relevant to me upon my reading of it.

(anything not explicitly defined I’ve already defined in my .hpp and if it’s supposed to have values I’ve already made and extracted them prior to this part of the code. I can include all of that if need be)
Here is how I’m reading these in

TFile *exp_tree_= new TFile(flags.Flags::Exp_File().c_str());
TFile *empty_tree_ = new TFile(flags.Flags::Empty_File().c_str());
TFile *hole_tree_ = new TFile(flags.Flags::Holes_File().c_str()); //This is the file I'm having problems with
for(int i=0; i<29; i++){
        for(int j=0; j<5; j++){
                sprintf(hname,"<exp hist name>",<relevant parameters>);
                std::cout<<"extracting " <<hname <<"\n";
               _exp_data_5d_pos[i][j] = (THnSparseD *)exp_tree_->Get(hname);
               _empty_5d_pos[i][j] = (THnSparseD *)empty_tree_->Get(hname);
               sprintf(hname, <THnSparse new name>",<relevant parameters>);
               std::cout<<"making " <<hname <<"\n";
               _N_5d_local_holes_pos[i][j] = (THnSparseD*)_exp_data_5d_pos[i][j]->Clone();
               _N_5d_local_holes_pos[i][j]->SetNameTitle(hname,hname);
               _N_5d_local_holes_pos[i][j]->Add(_empty_5d_pos[i][j],-flags_.Flags::Qr());//Empty target subtraction
              _N_5d_local_holes_pos[i][j]->Divide(_acceptance_5d[i][j]); 
              sprintf(hname,"<hole name>",<relevant parameters>);
              std::cout<<"adding " <<hname <<"\n";
             _N_5d_local_holes_pos[i][j]->Add((THnSparseD *)hole_tree->Get(hname));

What happens is I will get to i=20 and j = 1. I’ll print that last cout saying “adding hname” and then hit a segfault.

I feel like whatever the issue is it’s very obvious, but I cannot see it. I’d also rather not restructure everything unless I know that will actually solve the issue, as that will take some time.

Thank you,
Chris

ROOT Version:_6.26.04
Platform:* macOS Ventura 13.0
Compiler:* g++

Dear @cmclauchlin ,

Thanks for reaching out to the forum! From quickly reading your code snippet, the only “obvious” thing that comes to mind is that for i=20 and j=1 (THnSparseD *)hole_tree->Get(hname) returns a nullptr. I read that you made sure that this histogram was present in the file, but can you actually see it inside the for loop, e.g. can you store it in a variable and then print for example its name before passing it to the Add function?

Then, the warning you see about the file not being closed might just be related to the fact you are never closing the files in your program. From the snippet you sent above, you use new TFile without ever calling delete. This pattern should be avoided at all cost. In general, whenever you would write new TFile you should write std::unique_ptr<TFile> f{...}; instead so that the file will be automatically destroyed for you no matter what happens in your application.

If neither of this moves your situation forward, then I would encourage you to send us a full reproducer with code and data so we can further help you debugging.

Cheers,
Vincenzo

1 Like

Thank you for your help, @vpadulan

After making everything into unique pointers that resolved my issues completely! Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.