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:

[code]#include
#include
#include “TH1F.h”
#include “TFile.h”

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

return 0;
}[/code]

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 stackoverflow.com/questions/2751 … t-pointers

Hi Mustafa,

a good starting point is the documentation on the root site. In particular
root.cern.ch/drupal/content/root-users-guide-600
Chapter 8 “Object Ownership”.

Cheers,
Danilo

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.

Hi,

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!