Delete dynamically allocated TH1*'s

I have a

int script()
  TH1F *h = new TH1F("h1name", "h1name", 11, -5, 5);
  h -> FillRandom("gaus", 100);
  return 0;

I run the script as .x

Is there a general root command to call delete on dynamically allocated TH1*, to be used after the reference h has run out of scope? And a general command to delete all dynamically allocated objects (clear memory)?

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

root [0] .x
(int) 0
root [1] .ls
 OBJ: TH1F	h	h : 0 at: 0x7fb13cc60ce0
root [2] h->Draw()
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [3] h->Delete()
root [4] .ls
root [5] 

Is there a command to call delete on 100 (all) objects at once? Also, in this example h should be replaced with h1name.

You should hold/register the object’s address in a container (TObjArray for example) and delegate ownership to it.

TObjArray arr;
arr.SetOwner(); // or call arr.Delete() explicitly at the end
arr->Add( hist_ptr);
1 Like

what about gROOT -> Reset()?

The purpose of gROOT->Reset() is to (attempt) to completely undo all the operation done so far. I.e. if that is what you need, you may as well quit the session and restart :slight_smile:

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