Error when filling the self-defined class in root file

Hi all,

I’m working on a program that is required to record some self-defined data class into TTree. As following the example of “generating dictionary”, I succeeded to compile the program. But when the tree is filled, the whole program is crushed. The logic is like the following:

class Event : public TObject {
...
    SimulatedHit* hit;
    MCParticle* mcp;

} 

class SimualtedHit : public TObject {
    ...
    vector<MCParticle* > mc_vec;
}

class MCParticle : public TObject {
    ...
}

Every time when I push some elements back into the mc_vec, the program crashes. If I keep the vector empty, everything works fine.

The crush error is:

The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at http://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x00007f8b550e883b in TInstrumentedIsAProxy<McParticle>::operator()(void const*) () from /lustre/collider/zhangyulei/dark_photon/Simulation/DP/lib/libDEvent.so
#7  0x00007f8b5ab103a8 in TClass::GetActualClass(void const*) const () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libCore.so
#8  0x00007f8b5a18cd0c in TBufferIO::WriteObjectAny(void const*, TClass const*, bool) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#9  0x00007f8b5a1f79eb in TGenCollectionStreamer::WriteObjects(int, TBuffer&) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#10 0x00007f8b5a1f9749 in TGenCollectionStreamer::Streamer(TBuffer&) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#11 0x00007f8b5a1c9ef1 in TCollectionStreamer::Streamer(TBuffer&, void*, int, TClass*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#12 0x00007f8b5a1809a3 in TBufferFile::WriteFastArray(void*, TClass const*, int, TMemberStreamer*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#13 0x00007f8b5a3dad7b in int TStreamerInfo::WriteBufferAux<char**>(TBuffer&, char** const&, TStreamerInfo::TCompInfo* const*, int, int, int, int, int) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#14 0x00007f8b5a24249d in TStreamerInfoActions::GenericWriteAction(TBuffer&, void*, TStreamerInfoActions::TConfiguration const*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#15 0x00007f8b5a1866aa in TBufferFile::WriteClassBuffer(TClass const*, void*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#16 0x00007f8b5a186291 in TBufferFile::WriteObjectClass(void const*, TClass const*, bool) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#17 0x00007f8b5a18cd3b in TBufferIO::WriteObjectAny(void const*, TClass const*, bool) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#18 0x00007f8b5a1f79eb in TGenCollectionStreamer::WriteObjects(int, TBuffer&) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#19 0x00007f8b5a1f9749 in TGenCollectionStreamer::Streamer(TBuffer&) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#20 0x00007f8b5a1c9ef1 in TCollectionStreamer::Streamer(TBuffer&, void*, int, TClass*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#21 0x00007f8b5a3cce4f in int TStreamerInfo::WriteBufferAux<TVirtualCollectionProxy>(TBuffer&, TVirtualCollectionProxy const&, TStreamerInfo::TCompInfo* const*, int, int, int, int, int) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#22 0x00007f8b5a3c6e8f in TStreamerInfo::WriteBufferSTL(TBuffer&, TVirtualCollectionProxy*, int) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#23 0x00007f8b5a3db55c in int TStreamerInfo::WriteBufferAux<char**>(TBuffer&, char** const&, TStreamerInfo::TCompInfo* const*, int, int, int, int, int) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#24 0x00007f8b5a24249d in TStreamerInfoActions::GenericWriteAction(TBuffer&, void*, TStreamerInfoActions::TConfiguration const*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#25 0x00007f8b5a180485 in TBufferFile::ApplySequence(TStreamerInfoActions::TActionSequence const&, void*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libRIO.so
#26 0x00007f8b5893a3b2 in TBranch::FillImpl(ROOT::Internal::TBranchIMTHelper*) [clone .part.0] () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libTree.so
#27 0x00007f8b589478bb in TBranchElement::FillImpl(ROOT::Internal::TBranchIMTHelper*) () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libTree.so
#28 0x00007f8b589af4a6 in TTree::Fill() () from /cvmfs/sft.cern.ch/lcg/views/LCG_97rc4python3/x86_64-centos7-gcc9-opt/lib/libTree.so
#29 0x0000000000437143 in RootManager::FillSim(int, double const*) ()

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20/00
Platform: Mac OS 11.1 && CentOS 7
Compiler: gcc 9,2,0


There seems to be something amiss with the class MCParticle.

Did you generate a dictionary for it? Did you add the ClassDef? What are the data member and what is the implementation of the default constructor?

Hi pcanal,

The part of the LinkDef.h is:

#pragma link C++ class McParticle+;
#pragma link C++ class std::vector<McParticle*>+;

#pragma link C++ class SimulatedHit+;
#pragma link C++ class std::vector<SimulatedHit*>+;

For both SimulatedHit and McParticle class, I’ve added ClassDefOverride(McParticle,10) and ClassDefOverride(SimulatedHit,10)

If more information is needed, I can provide the gitlab repository.

Best,
Yulei

Okay, I’ve found the problem.

In the destructor of my SimulatedHit class,

SimulatedHit::~SimulatedHit() {
    // std::vector<McParticle* > MCPContribution;

    for (auto p: MCPContribution) delete p;   // add this line will lead to error
    MCPContribution.clear();
    MCPContribution.shrink_to_fit();
}

What is the relation between the MPContribution and the mc_vec you mentioned earlier? i.e. apriori the destructor was doing the right thing (delete the content and clear the container), so one reason I can see that it would cause a problem is if those same pointer (content of MPContribution) are *also* recorded in mc_vec (or any other containers).

Hi pcanal,

MPContribution is the mc_vec I mentioned before, which is std::vector<McParticle* >.
Yes, maybe. So I’ve changed McParticle* to McParticle.
Thanks so much!

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