Wrong object returned in ROOT when using auto keyword

If you use the keyword auto in the interpreter, it doesn’t correctly return (or at least print out the pointer) of the correct object. Bit of a gotcha.

For example, I have two instances of a class called Model loaded from two open files. I have a DataLoader class that handles the branch addresses etc. In any case, new DataLoader() creates a new member Model*.

root [1] DataLoader* dl = new DataLoader("r11-test3.root")
(DataLoader *) 0x7fe5e5485bf0
root [2] DataLoader* dl2 = new DataLoader("r11-test3b.root")
(DataLoader *) 0x7fe5e54dedc0
root [3] auto mt = dl->GetModelTree()
(TChain *) @0x7ffeed30a038
root [4] auto mt2 = dl2->GetModelTree()
(TChain *) @0x7ffeed30a038  //< WRONG
root [5] mt->GetEntry(0)
(int) 2085
root [6] mt2->GetEntry(0)  //< which one am I accessing?
(int) 2085
root [7] auto mod = dl->GetModel()
(Model *) @0x7ffeed30a038
root [8] auto mod2 = dl2->GetModel()
(Model *) @0x7ffeed30a038  //< WRONG

The second one gets the wrong object (on input 4). Using an explicit type though (same session, same variables), it does:

root [16] Model* mod4 = dl2->GetModel()
(Model *) 0x7fe5e54e08c0  //< CORRECT
root [17] TTree* mt2b = dl2->GetModelTree()
(TTree *) 0x7fe5e54e8640 //< CORRECT

In this case, I expect the data to be the same so I expect the same number of bytes to be loaded from GetEntry. So it’s ambiguous what I’m accessing.

root [19] mt2
(TChain *) @0x7ffeed30a038
root [20] mt
(TChain *) @0x7ffeed30a038
root [21] mt2b
(TTree *) 0x7fe5e54e8640

Perhaps this is intended behaviour? But it would be wrong in C++.

ROOT Version: 6.24/02
Platform: Mac OSX 11.4
Compiler: Apple clang version 12.0.5 (clang-1205.0.22.11) Target: x86_64-apple-darwin20.5.0

Hi @bleu65,

thanks for reporting this strange problem! I have opened a GitHub issue for it with a simple reproducer:

It seems that the pointer are actually still pointing to the correct object, it’s just that the interpreter prints out something wrong. So you can proceed with auto if you don’t care about the prints, or just use auto * which works as expected. So I hope you can continue working with that until one of our interpreter experts will take care about the issue on GitHub?


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