The program crash after all sentences have been executed

I wrote a program like below. It crash when the program end. What could be the problem?

///slewing.cpp
...
...
void slewing()
{
	...
	...
	cout << "Just before the end of the whole program" << endl;
}

Info in <TCanvas::Print>: Current canvas added to pdf file t0_RUN1_time_0_12_after.pdf
Info in <TCanvas::Print>: pdf file t0_RUN1_time_0_12_process.pdf has been closed
Info in <TCanvas::Print>: pdf file t0_RUN1_time_0_12_resolution_after.pdf has been closed
Warning in <TPDF::TPDF::EndObject>: No Object currently opened.
Info in <TCanvas::Print>: pdf file t0_RUN1_time_0_12_after.pdf has been closed
Warning in <TPDF::TPDF::EndObject>: No Object currently opened.
Just before the end of the whole program

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007fa2edd0bc3a in __GI___wait4 (pid=14676, stat_loc=stat_loc
entry=0x7fff788301e8, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
#1  0x00007fa2edd0bbfb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7fff788301e8, options=options
entry=0) at waitpid.c:38
#2  0x00007fa2edc7af67 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00007fa2ee32c0be in TUnixSystem::StackTrace() () from /home/llm/software/root_install/lib/libCore.so
#4  0x00007fa2ee328f65 in TUnixSystem::DispatchSignals(ESignals) () from /home/llm/software/root_install/lib/libCore.so
#5  <signal handler called>
#6  0x000055d719958b60 in ?? ()
#7  0x00007fa2ee282087 in TList::Delete(char const*) () from /home/llm/software/root_install/lib/libCore.so
#8  0x00007fa2ee21aaad in TROOT::EndOfProcessCleanups() () from /home/llm/software/root_install/lib/libCore.so
#9  0x00007fa2ee324ac3 in TUnixSystem::Exit(int, bool) () from /home/llm/software/root_install/lib/libCore.so
#10 0x00007fa2ee1c75cf in TApplication::Terminate(int) () from /home/llm/software/root_install/lib/libCore.so
#11 0x00007fa2ee5d5216 in TRint::Run(bool) () from /home/llm/software/root_install/lib/libRint.so
#12 0x000055d6d50ed2d3 in main ()
===========================================================


The lines below might hint at the cause of the crash. If you see question
marks as part of the stack trace, try to recompile with debugging information
enabled and export CLING_DEBUG=1 environment variable before running.
You may get help by asking at the ROOT forum https://root.cern/forum
preferably using the command (.forum bug) in the ROOT prompt.
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://root.cern/bugs or (preferably) using the command (.gh bug) in
the ROOT prompt. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x000055d719958b60 in ?? ()
#7  0x00007fa2ee282087 in TList::Delete(char const*) () from /home/llm/software/root_install/lib/libCore.so
#8  0x00007fa2ee21aaad in TROOT::EndOfProcessCleanups() () from /home/llm/software/root_install/lib/libCore.so
#9  0x00007fa2ee324ac3 in TUnixSystem::Exit(int, bool) () from /home/llm/software/root_install/lib/libCore.so
#10 0x00007fa2ee1c75cf in TApplication::Terminate(int) () from /home/llm/software/root_install/lib/libCore.so
#11 0x00007fa2ee5d5216 in TRint::Run(bool) () from /home/llm/software/root_install/lib/libRint.so
#12 0x000055d6d50ed2d3 in main ()
===========================================================

_ROOT Version: 6.30/06
_Platform: WSL2(ubuntu20.04.2)
_Compiler: linuxx8664gcc


Hi,

do you mind sharing the entire slewing.cpp?

I have upload the code and comment the sentences of output and made a small root file. The output is normal till the cout:Just before the end of the whole program
slewing.cpp (9.7 KB)
t0_RUN1_timing.root (30.1 KB)

Hmm, interesting. Your slewing.cpp can be reduced to just four lines of code and still it will segfault:

void simplified()
{
        TFile datafile("t0_RUN1_timing.root", "read");
        TTree *tree = datafile.Get<TTree>("time");
        for (Long64_t ev = 0; ev < tree->GetEntries(); ev++)
                tree->GetEntry(ev);
}

I haven’t seen this before. The same four lines of code work for any of my own files with a TTree (given, of course, that I change the file name and the TTree name).

Do you have any idea about it?
I have other many programs that have the same four lines and they work well. And I believe this is very common.
However, a new program with only this 4 lines will crash when the program is ending.

  for (Long64_t ev = 0; ev < tree->GetEntries(); ev++) {
    tree->GetEntry(ev);
    // do whatever you want with this event and then ...
    gROOT->GetListOfFunctions()->Remove(gROOT->GetFunction("f1"));
    gROOT->GetListOfFunctions()->Remove(gROOT->GetFunction("f2"));
  }

This doesn’t work for me.
Maybe you can have a look of the reply of @yus. It is really strange.

I just found that the program doesn’t crash when using ```root slewing.cpp". It crash when quitting ROOT.

$ root -l slewing.cpp
root [0]
Processing slewing.cpp...
10
Just before the end of the whole program
root [1] .q

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================

You must ensure that you “remove” both functions for every event (regardless of whether you use them in the “for” loop).
This should work in your “slewing.cpp”:

    tree->GetEntry(ev);
    // clean the list of functions first ...
    gROOT->GetListOfFunctions()->Remove(f1);
    gROOT->GetListOfFunctions()->Remove(f2);
    // ... and then process this event ...