TTree::Branch() vs. auto_ptr

Dear ROOT experts,

We are puzzled by the behavior we see when we moved from ROOT < 5.28 to ROOT > 5.28 (still the case in 5.33/01) concerning a branch created from something pointed to by an auto_ptr.

The basics are:

std::auto_ptr<test::TEST_STRUCT> data(new test::TEST_STRUCT);
// Create tree and branch.
TFile* f = new TFile("test.root", "recreate");
TTree* t = new TTree("test", "test");

// This segfaults in 5.26, works in 5.28.
//t->Branch("test.", "test::TEST_STRUCT", data.get());

// This works in 5.26, gives random results in 5.28.
t->Branch("test.", "test::TEST_STRUCT", &data);

I have attached a tarball with a small example. Instructions for use (after unpacking):

make clean && make install
./bin/test_write && root -b -q test_read.C

You will see that depending on the way the branch is created and on the ROOT version this will either produce the right results (testInt = 0 and testTimestamp = 1234567890), segfault or produce random numbers.

Are we doing something wrong with the ampersand trick? From the release notes we saw that in 5.28 there is official ROOT support for auto_ptr IO. It would be nice to understand what goes on in all four cases though.

Thanks in advance for your help.

Best regards,
Andrzej & Jeroen
test_auto_ptr_io.tar.gz (10.2 KB)


If I remember correctly in v5.26 TTree::Branch was only accepting the address of a pointer to the data.// This works in 5.26, gives random results in 5.28. t->Branch("test.", "test::TEST_STRUCT", &data);Thus, this was working in v5.26 only ‘by accident’ because the beginning of the auto_ptr happens to be the pointer to the data.

In v5.28, Branch was enhance to be able to automatically detect the type of its parameter and to accept in addition to the address of the pointer, the case where you pass directly the address of the data. i.e.: t->Branch("test.", "test::TEST_STRUCT", data.get());is supported for v5.28 and up and can even be simplified to: t->Branch("test.", data.get());.


Thank you Philippe,

If I understand correctly, &data is something we should have avoided in the first place. Makes sense, actually. Guess we’ve been lucky so far.