Add a branch holding object

Hello root community

I am trying to compile a program which used to run on older version of roots, but now it is giving me error while adding object to a branch.

here is description

‘’’
SS1 yt; //ss1 is class
SS1 *ipyt=&yt; //it works if I use nullptr instead of &yt
tree->Branch(“yt.”,&ipyt,32000,99);

‘’’
the error is something like
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)

[/Users/msingh/packages/root/lib/libTree.so] TTree::BranchImp(char const*, TClass*, void*, int, int) (no debug info)

[/Users/msingh/packages/bin] TBranch* TTree::Branch(char const*, SS1**, int, int) /Users/msingh/packages/root/include/TTree.h:369

ROOT Version: 6.24
Platform: MacOs M1
Compiler: Not Provided


When writing:
SS1 yt; tree->Branch("yt.", &yt, 32000, 99);

When reading (in any case, you must “delete ipyt;” in the end):
SS1 *ipyt = 0; tree->SetBranchAddress("yt.", &ipyt);
or:
SS1 *ipyt = new SS1(); tree->SetBranchAddress("yt.", &ipyt);

1 Like
void HandleBOR_Scaler(int run, int gFileNumber, int time, IScaler *pscaler)
{
....
else tree->SetBranchAddress("scaler",&pscaler);
...
}

Unfortunately this pattern is a “disaster” :frowning:

This is asking the TTree to record and later use the address of the function variable. After the end of the function execution, the address will be reused for other purpose but the TTree (when GetEntry is called) will read and write at that address with … random and usually catastrophic (in the best case scenario you get a segmentation fault but at a weird looking place).

To partially solve the problem you would both needs to change the paratemer type from a pointer to a reference to a pointer:

void HandleBOR_Scaler(int run, int gFileNumber, int time, IScaler *pscaler)

AND make sure that HandleBOR_Scaler is called with a pointer variable whose lifetime is greater or equal to the lifetime of the TTree.

Cheers,
Philippe.

I guess, @pcanal wanted to say:
void HandleBOR_Scaler(int run, int gFileNumber, int time, IScaler *&pscaler)

Wile is right ! I missed the edit after the copy/paste. I have now updated the original post.