Home | News | Documentation | Download

What to do with a TH1* from file no longer needed

Consider this example:

TFile *file = TFile::Open("file.root", "READ");
TH1 * h = (TH1*) file -> Get("histo");

If I would like to remove this histogram from the heap memory is it sufficient to do:

h -> Delete();
h = nullptr;

Please read tips for efficient and successful posting and posting code

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

yes that should be enough, or even just delete h.


what is the main difference between

delete h


h -> Delete()

I don’t think there is any difference in practice if the object is on the heap, @pcanal @Axel ?

If the object is on the stack, ->Delete() should still be safe (and a no-op), while you can’t call delete on a stack-allocated object.

Please just use delete h which works for any heap-allocated object, like those read from a TFile.

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