Hello, I have been really looking for an answer for weeks on this issue.
Originally I had created a TTree with 2 branches containing two custom classes. This works fine. I use CINT to generate dictionaries for the custom classes in the custom built tool of each .h file.
Then I tried to create a 3rd branch just like the other two, with the only difference that inside the class attached to TBranch (class A below) there is an element vector b;
The code compiles fine but then I get a seg fault or exception when executing the TBranch statement.
I dumbed down the problem to a trivial class B containint only an integer.
When I replace vector b; in class A with just B b; TBranch does not crash. No matter what random combinations I tried in linkdef or use pointer definitions such as vector *b;
the program crashes at InspectElement, or BranchImp. After reading various not-so-well matching posts, I removed ClassImp from the cpp files. Nothing helped. I paste below the offending parts of the code.
ROOT Version: 5.34.09
Platform: Windows 10
Compiler: VS2019 C++
#pragma once
#include <TObject.h>
#include <vector>
class B : public TObject
{
public:
B();
~B();
int i;
ClassDef(B, 1);
};
///
class A :
public TObject
{
public:
A();
~A();
TString moduleName;
int version;
TString dateCreated;
TString creator;
TString notes;
std::vector<B> b; //this doesnt work
//B b;//this works
ClassDef(A, 1);
};
.cpp files only have some trivial initialisations of the integers and stirngs.
A_linkdef.h
#ifdef __CINT__
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ nestedclass;
#pragma link C++ nestedtypedef;
//#pragma link C++ class B+; //this causes an 'already defined error during compilation
#pragma link C++ class A+;
#pragma link C++ class vector<B>+;
#endif
B linkdef
#ifdef __CINT__
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
#pragma link C++ nestedclass;
#pragma link C++ nestedtypedef;
#pragma link C++ class vector<B>+; //this was added in the end but made no difference
#pragma link C++ class B+;
#endif
In main:
variables declared in the global scope
main(){
recordArchive = new TTree("RecordArchive", "Record Archive");
record = new Record();
baseData = new BaseData();
a = new A();
brBase = recordArchive->Branch("Base Data", "BaseData", &baseData);
brRecords = recordArchive->Branch("Records", "Record", &record);
brA = recordArchive->Branch("A A", "A", &a); //this crashes when A contains vector<B> b; but not when it just contains B b;
}
Advice appreciated. Thanks