Crash when filling a tree with object containing pointers

Hi there,

This is the complete crashing script for testing crash.C (475 Bytes), now let’s walk along it.

At first, let’s create an empty class derived from TObject:

   class Inner : public TObject { };

Next, create the class holding a pointer to Inner. Also, define custom simple copy constructor as well as the assignment operator (which intentionally don’t really copy anything, but rather create a fresh object and leave object unchanged on assignment, to prevent from pointer copying and potential double-free):

   class Outer {
      Inner *inner = 0;
         inner = new Inner();
      Outer(const Outer& other)
         inner = new Inner();
      Outer& operator=(const Outer& other)
         return *this;
      virtual ~Outer()
         delete inner;

Now, create a tree and also a branch holding Outer, then try to add an entry to tree:

   TTree t;
   Outer *in_ptr = 0;

   t.Branch("TheBranch", "Outer", &in_ptr);

It ends up with crash segfault.txt (6.2 KB)

However, if I replace field’s pointer type Inner with just TObject, i.e. replace

      Inner *inner = 0;


      TObject *inner = 0;

– it doesn’t crash anymore.

So I’m probably supposed to put some additional code into Inner to add its instances to a tree, right? What exactly should I do to handle an Inner field properly?


ROOT Version: 6.18.04
Platform: macOS, Linux Mint 19.2
Compiler: clang 1001.0.46.4, g++ 7.4.0

A class derived from TObject must have a ClassDef and you must generated a dictionary for that class in order for the class to be fully functional.

Well, so I see I should learn the ‘Adding a Class’ chapter of User Guide carefully… Thank you!

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