Re: Deleting TObjArray

From: Axel Naumann <Axel.Naumann_at_cern.ch>
Date: Mon, 22 Oct 2007 18:10:48 +0200


Hi Chiara,

TObjArray *fCal = new TObjArray(10);
fCal->SetOwner();
for (Int_t ich = 0; ich<10; ich ++){

   AliTOFChannelOnline * calChOnline = new AliTOFChannelOnline();    fCal->AddAt(calChOnline,ich);
}
delete fCal;
fCal=0;

is sufficient. calChOnline will still be !=0 because it still points to memory address of the latest allocated AliTOFChannelOnline object - even though this memory is now invalid because the object got deleted by deleting the TObjArray in was contained in. So it's all fine.

Alternatively you could also use a std::vector<AliTOFChannelOnline>.

Cheers, Axel.

Chiara Zampolli wrote:
> Dear rooters,
>
> I have a doubt on how to delete a TObjArray and all its elements.
> What I am doing is:
>
> TObjArray *fCal = new TObjArray(10);
> fCal->SetOwner();
> for (Int_t ich = 0; ich<10; ich ++){
> AliTOFChannelOnline * calChOnline = new AliTOFChannelOnline();
> fCal->AddAt(calChOnline,ich);
> }
> fCal->Clear();
> delete fCal;
> fCal=0x0;
>
> Is this the correct way to delete the TObjArray and all its elements? I
> thought yes, also from the ROOT user's guide. In fact, I still have some
> doubts, because if I do this, and then print the address of calChOnline,
> this is not null, and since there are some crashes while running a macro
> which uses this new/delete schema, I was wondering whether the problem
> could be here...
>
> Thanks for your help.
> Cheers,
> Chiara
>
>
Received on Mon Oct 22 2007 - 18:11:15 CEST

This archive was generated by hypermail 2.2.0 : Tue Oct 23 2007 - 11:50:01 CEST