# Add a branch holding object

**URL:** <https://root-forum.cern.ch/t/add-a-branch-holding-object/47093>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [October 3, 2021, 7:59pm UTC](https://root-forum.cern.ch/t/add-a-branch-holding-object/47093 "2021-10-03T19:59:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![singh](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/singh/32/26508_2.png) [@singh](https://root-forum.cern.ch/u/singh)\
**Post date:** [October 3, 2021, 7:59pm UTC](https://root-forum.cern.ch/t/add-a-branch-holding-object/47093/1 "2021-10-03T19:59:41Z")

</div>

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

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 4, 2021, 6:55am UTC](https://root-forum.cern.ch/t/add-a-branch-holding-object/47093/2 "2021-10-04T06:55:23Z")

</div>

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);`

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 4, 2021, 11:45pm UTC](https://root-forum.cern.ch/t/add-a-branch-holding-object/47093/4 "2021-10-04T23:45:12Z")

</div>

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

```

Unfortunately this pattern is a “disaster” ☹ …

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 5, 2021, 6:57am UTC](https://root-forum.cern.ch/t/add-a-branch-holding-object/47093/6 "2021-10-05T06:57:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 5, 2021, 4:34pm UTC](https://root-forum.cern.ch/t/add-a-branch-holding-object/47093/7 "2021-10-05T16:34:58Z")

</div>

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