// // Run this in ROOT with // // Root> .x bar.cxx++ // #ifndef __CINT__ #ifndef ROOT_TObject #include "TObject.h" #endif #ifndef ROOT_TObjArray #include "TObjArray.h" #endif #ifndef __IOSTREAM__ #include #endif #ifndef __IOMANIP__ #include #endif #endif // __CINT__ class C1 : public TObject { private: Int_t fInt; public: C1(Int_t i=0) : fInt(i) {} void Print(Option_t* option="") const { cout << "C1 object: " << fInt << " " << hex << this << endl; } } ; class C2 : public TObject { private: C1* fC1; public: C2(C1* c1=0) : fC1(c1) {} // ~C2() { delete fC1; } ~C2() {} void Print(Option_t* option="") const { cout << "C2 object: " << hex << fC1 << endl; } } ; void bar() { cout << "Create array with 5 C1 objects in it" << endl; TObjArray array; for (Int_t i = 1; i <= 5; i++) array.Add(new C1(i)); array.Print(); cout << "Making a C2 object and deleting it" << endl; C2* c = new C2((C1*)array.At(3)); c->Print(); delete c; cout << "Will get SIGSEGV here if C2::~C2 deallocates pointer" << endl; array.Print(); cout << "Cleaning up the array" << endl; array.Delete(); array.Print(); }