Hi Alberto, In your C2 destructor, you have nothing to do. You should not delete the C1 object ! I take this opportunity to mention 2 interesting classes introduced in 3.02 TRef and TRefArray. See: http://root.cern.ch/root/htmldoc/TRef.html http://root.cern.ch/root/htmldoc/TRefArray.html Rene Brun Alberto Pulvirenti wrote: > > Dear all (but maybe it's Rene who knows the answer) > > I've a little question about dynamic memory allocation. Follow this > example to understand what the question is. > > I have two classes, say 'C1' and 'C2', inheriting from TObject. In > particular, in the C2 is defined a data-member which points to a C1 object: > > class C2 : public TObject { > > . > . > . > > public: > > . > . > . > > C1 *fPointer; > > . > . > . > > }; > > In my work, I have created a TObjArray, say obj_arr, where I stored many > objects of class C1. When I create a C2 object, I initialize its > fPointer data-member to point to one of the elements of the 'obj_arr' > collection: > > C2 *c = new C2; > c->fPointer = (C1*)obj_arr->At(k); > > However, I need to delete the C2 object, without deleting the C1 object > it points to. Then, what must I put in the C2 destructor? > > If I use this one: > > C2::~C2() { > delete fPointer; > } > > will it erase the memory, cancelling the C1 object even if I keep in RAM > the TObjArray where it was initially stored? > Or this will remove only the link, but, given that there is another > class which points to that memory slot (obj_arr, in this case), the C1 > object will be kept and not deleted? > > The question comes from the observation I did that, without using the > destructor I presented you, it seems to come a large memory leak, so I > thought it was necessary to use it... > > If I was clear, could you give me a hint about this question? > > Regards by Alberto
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:09 MET