Does root clean up memory of new'd objects?

I was just wondering if root deletes objects that are created on the heap.

When I write code for root, I usually do something like

void someFunction{ TH1D* hist = new TH1D(...); hist->Fill(...); hist->Draw(); // no delete hist !! }
because I want to see the histogram on the screen, i.e. it should survive beyond the scope of the function. I also usually never delete my histograms. Is this wrong?
Or is this histogram properly deleted at some point? When?
Would it be better to write

void someFunction{ TH1D* hist = new TH1D(...); hist->Fill(...); hist->DrawCopy(); delete hist; }

Related to this question, I found this: … rship.html but I am missing an example to really understand the consequences of object ownership in root.


In the case of histogram, by default, the ownership falls to the ‘current’ ROOT directory. If you do not create/open any TFile, it default to gROOT. So, yes (unless you call TH1::AddDirectory(0) to signify that you want to take ownership), the histogram are owned (and will be eventually deleted by) TROOT (or any TFile that you create).