ROOT's Objects Ownership and C++11 Smart Pointers

I am trying to understand how smart pointers are expected to live with ROOT object ownership scheme. For example:

#include “TH1F.h”
#include “TFile.h”

int main()
TFile f(“out.root”, “recreate”);;
std::unique_ptr h {new TH1F(“h”, “h”, 100, -5, 5)};
h->FillRandom(“gaus”, 10000);

return 0;

The histogram which is handled by a unique pointer was owned by the current gDirectory. Since I politely closed the file before I exit my program the histogram was destroyed by ROOT memory management guy. Now at the end of main() my pointer goes out of scope and its resource needs to be freed, but it has already been freed!

I haven’t found any resources on how ROOT object ownership/memory management is expected to live with C++11 smart pointers.

I want to understand what are the different experiments’/users’ guidelines on using smart pointers in code that handles ROOT objects. If the modern C++ code is expected to get rid of naked pointers then we expect to see more smart pointers in programmers’ codes. Should we just ban the use of smart pointers to handle ROOT objects? Turn off ROOT objects management? Other plans from ROOT to get around this?

P.S. I also posted the question on StackOverflow … t-pointers

Hi Mustafa,

a good starting point is the documentation on the root site. In particular
Chapter 8 “Object Ownership”.


Hi Danilo,

Thanks for the link. I am fully aware of those details. That is exactly why I am asking about the plan for ROOT ownership in light of the modern C++ idiom of no naked pointers.


All of us agree :slight_smile: There is a clash of cultures - C++ from the 90s and C++11.

We are planning on addressing that; I hope that you will see first traces of that next year.

Cheers, Axel.

Looking forward to it.
Thanks Alex!