Home | News | Documentation | Download

Segmentation fault when removing object from TRefArray


ROOT Version: 6.22/02
Platform: Ubuntu 20.04
Compiler: gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0


I am trying to fill a TTree with an Event object whose component objects contain references (TRefArray) among each other, but I get a segmentation fault when calling TTree::Fill, if and only if I add AND THEN remove one of the references.

In summary this is what triggers the segfault:

  1. Open a TFile
  2. Create a TTree containing a custom Event class
  3. The Event class contains a collection of objects of Hit and Track classes
  4. Add a Hit to a Track
  5. Remove the Hit from the Track
  6. Call TTree::Fill

Because talk is cheap, I have created a minimal reproducer. You just have to run the compile.sh script and then you should see the segfault message. test.zip (2.9 KB)

Please let me know if you prefer me to submit this as a bug in the ROOT bug tracker, or tell me what I am doing wrong.

Thanks for you report/question. I’ll take a look.

After Removing the element and before call Fill you need to “Compress” the array:

 {
    Track * track1 = event.AddTrack();
    Hit * hit1 = event.AddHit();
    hit1->parent_track = track1;
    track1->hits.Add((TObject *) hit1);

    Track * track2 = event.AddTrack();
    Hit * hit2 = event.AddHit();
    hit2->parent_track = track2;
    track2->hits.Add((TObject *) hit2);
    track2->parent_track = track1;

    event.RemoveTrack(track1);
    event.tracks->Compress();

    ttree.Fill();
  }
  {
    Track * track1 = event.AddTrack();
    Hit * hit1 = event.AddHit();
    hit1->parent_track = track1;
    track1->hits.Add((TObject *) hit1);

    Track * track2 = event.AddTrack();
    Hit * hit2 = event.AddHit();
    hit2->parent_track = track2;
    track2->hits.Add((TObject *) hit2);
    track2->parent_track = track1;

    event.RemoveTrack(track1);
    event.RemoveTrack(track2);
    event.tracks->Compress();
  }
1 Like

Thank you very much for looking into this issue. I will try this solution in the actual application and let you know.

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