Delete file crash


I produced a class with the MakeClass function.

In the Init(TTree tree) I read some histograms from a root file and I store
them into a
> refHisto*

In my main; to use this class I just do something like:

  TChain* bckgT = new TChain("TreeB");
  TChain* signT = new TChain("TreeS");
  //   BCKG
  Boosted *bckg = new Boosted(bckgT);
  // SIGN
  Boosted *sign = new Boosted(signT);

  delete bckg;
  delete sign;

  return 0;

I am compiling it with g++

the first problem is the usual:

Warning in TDirectoryFile::Append: Replacing existing TH1: histo0 (Potential memory leak).

that appears when I call the second constructor but I guess there is nothing I can really do since I am really creating two object with the same
name. They will be stored in a public member of the class so I don’t think I have a memory leak.

The real problem is that it crashes at the end of the main (after the delete sign;) with this error

#10 0x0025e4bd in TString::TString(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /afs/ #11 0x002e06b2 in ROOT::TGenericClassInfo::CreateRuleSet(std::vector<ROOT::TSchemaHelper, std::allocator<ROOT::TSchemaHelper> >&, bool) () from /afs/ #12 0x002e0b49 in ROOT::TGenericClassInfo::GetClass() () from /afs/ #13 0x02fa76c4 in TProfile::Class() () from /afs/ #14 0x02dc0c37 in TProfile::IsA() const () from /afs/ #15 0x00238474 in TObject::InheritsFrom(TClass const*) const () from /afs/ #16 0x010270c5 in TDirectoryFile::Save() () from /afs/ #17 0x0102711b in TDirectoryFile::Save() () from /afs/ #18 0x0102230b in TDirectoryFile::Close(char const*) () from /afs/ #19 0x0103e7d4 in TFile::Close(char const*) () from /afs/ #20 0x0103eb84 in TFile::~TFile() () from /afs/ #21 0x002a4c2d in TCollection::GarbageCollect(TObject*) () from /afs/ #22 0x002abbd0 in TList::Delete(char const*) () from /afs/ #23 0x00254c81 in TROOT::~TROOT() () from /afs/ #24 0x01a084b9 in __cxa_finalize () from /lib/i686/nosegneg/ #25 0x002020c4 in __do_global_dtors_aux () from /afs/ #26 0x0068d810 in _fini () from /afs/ #27 0x0098a956 in _dl_fini () from /lib/ #28 0x01a08209 in exit () from /lib/i686/nosegneg/ #29 0x019f1ea4 in __libc_start_main () from /lib/i686/nosegneg/ #30 0x0804b8a1 in _start ()

it is clear that it’s a problem of deleting the TFile I opened. I tried different solutions, i.e. deleting the file in the descructor but it didn’t help.
How am I supposed to delete the file?

I have never had this problem when doing this in macros.



I run valgrind and new hints came up related to CleanUpRooFitAtExit()

at 0x41346AA: TObject::~TObject() (in /afs/ ==2122== by 0x61080DF: RooAbsCollection::~RooAbsCollection() (in /afs/ ==2122== by 0x61796E4: RooArgSet::~RooArgSet() (in /afs/ ==2122== by 0x627D91F: RooPlot::~RooPlot() (in /afs/ ==2122== by 0x419FC2C: TCollection::GarbageCollect(TObject*) (in /afs/ ==2122== by 0x41A2B86: THashList::Delete(char const*) (in /afs/ ==2122== by 0x411C04A: TDirectory::~TDirectory() (in /afs/ ==2122== by 0x4D65D6D: TDirectoryFile::~TDirectoryFile() (in /afs/ ==2122== by 0x419FC2C: TCollection::GarbageCollect(TObject*) (in /afs/ ==2122== by 0x41A2B86: THashList::Delete(char const*) (in /afs/ ==2122== by 0x4D65362: TDirectoryFile::Close(char const*) (in /afs/ ==2122== by 0x4D817D3: TFile::Close(char const*) (in /afs/ ==2122== Address 0x10ddfcbc is 4,188 bytes inside a block of size 1,048,576 free'd ==2122== at 0x4006516: free (vg_replace_malloc.c:325) ==2122== by 0x6179BA2: RooArgSet::cleanup() (in /afs/ ==2122== by 0x62C614E: CleanUpRooFitAtExit() (in /afs/

it sounds a bit obscure to me but is it possible that when “cleaning up” it also destroy the file I opened?
It seems to be triggered by a ~RooPlot(). Commenting out all the RooPlot objects it dosen’t crash anymore


ok I probably solved the problem.
I had to delete all the RooPlot objects I created with

RooPlot *xframe=Mass->frame(nbin);
I was missing one…



the initial problem re-appered adding or changing some code.
Looking at the initial stack trace I noticed that the problem was related with a TProfile object.
I somehow understood that my TProfiles were descructed by the discructor of my class (~Boosted) but also
when distructing the file from which I was reading some histogram.
Just adding

solved my problem.
I don’t know why I don’t have to do this for all the other objects such as TH1.