Using TTreeReader for TTimeStamp/TDatime objects

Hi there,

When trying to read a TTimeStamp object stored on a TTree which is coming from a function, ROOT crashes with some message like /include/TTreeReaderValue.h:139:14: warning: invalid memory pointer passed to a callee: return fProxy->IsaPointer() ? *(T**)address : (T*)address; but I see the function returning a valid pointer to the TTree. After some work, I came up with a way to reproduce the issue:

/// TestTS.C

TTree *GetTree(){
  
  TTree *t = new TTree("t","t");
  
  TTimeStamp date;
  TBranch *fTimeStamp = t->Branch("fTimeStamp", &date);
  
  for(Int_t i = 1; i < 30; i++){
    date = TTimeStamp(2018,01,i,00,00,00,0);
    t->Fill();
  }
  
  return t;

}

Int_t TestTS(){
  
  TTree *tree = GetTree();

  // Make sure
  tree->Print();

  if(tree){
    TTreeReader fReader(tree);
    TTreeReaderValue<TTimeStamp> fTS(fReader,"fTimeStamp");
  
    while(fReader.Next()){
      printf("%s\n", fTS->AsString());
    }
  }
  
  return 0;

}

Do you guys have any idea where I can look up to fix it? If I define everything in the same function there’s no problem, so my guess is it is something about the scope of the pointer, but I’m having a hard time trying to figure out how to fix it. Any suggestion would be appreciated. :+1:

Related:

Hi,

what ROOT verison is this? The master seems to be unaffected*

Cheers,
D

   --------------------------------------------------------------------------
  | Welcome to ROOT 6.13/01                              http://root.cern.ch |
  |                                             (c) 1995-2017, The ROOT Team |
  | Built for macosx64                                                       |
  | From heads/FixROOT-8127@v6-11-02-1286-gc56d0d1c99, Jan 04 2018, 07:15:06 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'               |
   --------------------------------------------------------------------------


Processing TestTS.C...
******************************************************************************
*Tree    :t         : t                                                      *
*Entries :       29 : Total =            2492 bytes  File  Size =          0 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Branch  :fTimeStamp                                                         *
*Entries :       29 : BranchElement (see below)                              *
*............................................................................*
*Br    0 :fSec      : Int_t                                                  *
*Entries :       29 : Total  Size=        753 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
*Br    1 :fNanoSec  : Int_t                                                  *
*Entries :       29 : Total  Size=        777 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
Wed, 30 Mar 2022 10:33:04 UTC +       35 nsec
(int) 0

In your “GetTree” function, either define:

static TTimeStamp date;

or, right before return t;, add:

t->ResetBranchAddresses(); // "disconnect" from local variables

Hi @Wile_E_Coyote, Thank you! That rocks!. :love_you_gesture:

From the docs:

Note: The pointer whose address is passed to TTree::Branch must not be destroyed (i.e. go out of scope) until the TTree is deleted or TTree::ResetBranchAddress is called.

Hi @Danilo, it is root 6.10/04 . Are you using the same script? Because I see it is printing the same date and it is not the one that is being assigned.