Problem retrieving class object from file

I have created my own library which implements the THit class. The THit class stores some basic information and is defined as follows:

[code]
#include "TObject.h"
class THit : public TObject {

public:
THit();
~THit();
void Initialize();

//Sets
void setEnergyDeposited(Double_t);
void setTimeOfHit(Double_t);
void setParticleType(const Char_t *);
void setParticleEnergy(Double_t);
void setParticleMomentum(Double_t, Double_t, Double_t);
void setParticleVelocity(Double_t, Double_t, Double_t);
void setParticleLocalPosition(Double_t, Double_t, Double_t);
void setVolumeName(const Char_t *);
void setVolumeMother(const Char_t *);
void setVolumeMaterial(const Char_t *);

//Gets
Double_t getEnergyDeposited();
Double_t getTimeOfHit();
const Char_t * getParticleType();
Double_t getParticleEnergy();  
Double_t getParticleMomentum();  //Only returns x component for now
Double_t getParticleVelocity(); //see above
Double_t getParticleLocalPosition(); //see above
const Char_t * getVolumeName();
const Char_t * getVolumeMother();
const Char_t * getVolumeMaterial();

private:

Double_t energyDeposited;
Double_t timeOfHit;
const Char_t * particleType;
Double_t particleEnergy;
Double_t px;
Double_t py;
Double_t pz;
Double_t vx;
Double_t vy;
Double_t vz;
Double_t lx;
Double_t ly;
Double_t lz;  
const Char_t * volumeName;
const Char_t * volumeMother;
const Char_t * volumeMaterial;

ClassDef(THit,1); // THit Class

};[/code]

I can load my library into root and create a THit object. All of the functions implemented are working correctly. For example:

THit * hit = new THit();
hit->Initialize();
hit->setEnergyDeposited(10.0);
hit->Dump();

That displays all of the correct values. I can then write this to a file:

TFile f("out.root","CREATE");
hit->Write("hit");
f.Close();

I then start a new root session and open the file and try and retrieve my object.

TFile f("out.root");
THit * hit = (THit*)f.Get("hit");

All is good. Now assume I made two hit objects and wrote them both in the same fashion to the same file. I’ll call them hit1 and hit2. If I open the file, I can load hit1, but I get a segmentation fault when I try to load hit2, or if I load hit2 first I get the problem when I load hit1. The stack trace is included below. I build my library with the following commands, for reference:

g++ -fPIC -c $(HITS)/src/THit.cc -o $(HITS)/tmp/THit.o -I $(HITS)/include -I $(G4INCLUDE) -I $(ROOT_INCLUDE) -g
rootcint $(HITS)/src/hitDict.cc -c $(HITS)/include/THit.h $(HITS)/include/LinkDef.h
g++ -fPIC -c $(HITS)/src/hitDict.cc -o $(HITS)/tmp/hitDict.o -I $(HITS)/include -I $(G4INCLUDE) -I $(ROOT_INCLUDE) -g
g++ -shared -o $(HITS)/lib/librhits.so $(HITS)/tmp/THit.o $(HITS)/tmp/hitDict.o

And although I haven’t explicitly shown the .L to load my library in my commands, I’m doing it each time I start up root.

Thanks in advance for any help. It is much appreciated.

root [4] THit * hit1 = (THit*)f.Get("hit1");

 *** Break *** segmentation violation
 Generating stack trace...
 0x4138daf1 in operator delete(void*) + 0x21 from /usr/lib/libstdc++.so.5
 0x4138db4d in operator delete[](void*) + 0x1d from /usr/lib/libstdc++.so.5
 0x40214c60 in int TStreamerInfo::ReadBuffer<char**>(TBuffer&, char** const&, int, int, int, int) + 0x3288 from /usr/local/cern/root/lib/libCore.so
 0x401f7c73 in TClass::ReadBuffer(TBuffer&, void*) + 0x205 from /usr/local/cern/root/lib/libCore.so
 0x41cc576f in THit::Streamer(TBuffer&) at /home/walker/test/packages/hits/src/hitDict.cc:104 from /home/walker/test/packages/hits/lib/librhits.so
 0x401603be in TKey::ReadObj() + 0x388 from /usr/local/cern/root/lib/libCore.so
 0x4014c39c in TDirectory::Get(char const*) + 0x1ee from /usr/local/cern/root/lib/libCore.so
 0x402cb667 in <unknown> from /usr/local/cern/root/lib/libCore.so
 0x407c9ada in G__call_cppfunc + 0x2db from /usr/local/cern/root/lib/libCint.so
 0x407b840c in G__interpret_func + 0x804 from /usr/local/cern/root/lib/libCint.so
 0x40799663 in G__getfunction + 0x15d0 from /usr/local/cern/root/lib/libCint.so
 0x40830fe7 in G__getstructmem + 0x931 from /usr/local/cern/root/lib/libCint.so
 0x40828df3 in G__getvariable + 0x62c from /usr/local/cern/root/lib/libCint.so
 0x40790055 in G__getitem + 0x55b from /usr/local/cern/root/lib/libCint.so
 0x4078ea1b in G__getexpr + 0x866a from /usr/local/cern/root/lib/libCint.so
 0x40798edf in G__getfunction + 0xe4c from /usr/local/cern/root/lib/libCint.so
 0x40790081 in G__getitem + 0x587 from /usr/local/cern/root/lib/libCint.so
 0x4078ea1b in G__getexpr + 0x866a from /usr/local/cern/root/lib/libCint.so
 0x4077ba6b in G__define_var + 0x21ec from /usr/local/cern/root/lib/libCint.so
 0x40819c53 in G__defined_type + 0x57c from /usr/local/cern/root/lib/libCint.so
 0x407e8ad8 in G__exec_statement + 0x1ae7 from /usr/local/cern/root/lib/libCint.so
 0x4077619f in G__exec_tempfile_core + 0x32a from /usr/local/cern/root/lib/libCint.so
 0x407763da in G__exec_tempfile_fp + 0x22 from /usr/local/cern/root/lib/libCint.so
 0x407f1f72 in G__process_cmd + 0x490e from /usr/local/cern/root/lib/libCint.so
 0x401e8b97 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0xaf from /usr/local/cern/root/lib/libCore.so
 0x40134d28 in TApplication::ProcessLine(char const*, bool, int*) + 0x66e from /usr/local/cern/root/lib/libCore.so
 0x412c36a2 in TRint::HandleTermInput() + 0x1f8 from /usr/local/cern/root/lib/libRint.so
 0x412c2272 in TTermInputHandler::Notify() + 0x24 from /usr/local/cern/root/lib/libRint.so
 0x412c41be in TTermInputHandler::ReadNotify() + 0x12 from /usr/local/cern/root/lib/libRint.so
 0x40289eae in TUnixSystem::CheckDescriptors() + 0x152 from /usr/local/cern/root/lib/libCore.so
 0x40288dbf in TUnixSystem::DispatchOneEvent(bool) + 0x17d from /usr/local/cern/root/lib/libCore.so
 0x401a5a68 in TSystem::InnerLoop() + 0x18 from /usr/local/cern/root/lib/libCore.so
 0x401a5a07 in TSystem::Run() + 0x7b from /usr/local/cern/root/lib/libCore.so
 0x40135532 in TApplication::Run(bool) + 0x32 from /usr/local/cern/root/lib/libCore.so
 0x412c30e7 in TRint::Run(bool) + 0x3e5 from /usr/local/cern/root/lib/libRint.so
 0x08048e1d in main + 0x71 from /usr/local/cern/root/bin/root.exe
 0x41408b10 in __libc_start_main + 0xe0 from /lib/tls/libc.so.6
 0x08048d11 in TApplicationImp::ShowMembers(TMemberInspector&, char*) + 0x3d from /usr/local/cern/root/bin/root.exe

Hi,

The most likely cause is that your default constructor does not properly initialize the data member which are pointer (they should be set to zero explicitly).

Cheers,
Philippe.