Re: cleanup of objects

Rene Brun (Rene.Brun@cern.ch)
Sat, 19 Sep 1998 01:44:59 +0200


Eddy Offermann wrote:
>
> Dear Root-ers,
>
> Below are two macro's. The first one creates a root file with
> a directory and a tree and the second one reads it many times.
> This should simulate a 'real' application where several root
> files contain directories in which tree's have been created with
> the same name.
>
> My problem is that I can not figure out how to clean up after,
> getting the data from a file. The "delete dir;" is resulting
> in a segmentation violation. Removing this statement makes
> root use more and more memory.
>
> Is there a way to avoid this memory leak ?
>
> best regards,
> Eddy
>

Eddy,
I have modified your second macro. You should not do file->Get("dir").
Simply change the directory. Deleting the file containing the Tree
will also delete the Tree object from memory.

Rene Brun

{
for (Int_t i=1; i >0; i++)
{
TFile *hfile = new TFile("foo.root","READ");
hfile->cd("dir");

TTree *tree = (TTree*) dir->Get("T");

Int_t tmp;
tree->SetBranchAddress("B",&tmp);
tree->GetEvent(0);
printf("i tmp = %d %d\n",i,tmp);

delete hfile;
}
}

> {
> gROOT->Reset();
>
> Int_t split = 0;
> Int_t bsize = 32000;
>
> TFile *hfile = new TFile("foo.root","RECREATE");
>
> TDirectory *dir = hfile->mkdir("dir");
> dir->cd();
>
> TTree *tree = new TTree( "T", "this is a tree" );
>
> Int_t tmp = 100;
> TBranch *branch = tree->Branch("B",&tmp,"tmp/I",bsize);
> tree->Fill();
>
> hfile->Write();
> hfile->Close();
> delete hfile;
> }
>
> {
> for (Int_t i=1; i >0; i++)
> {
> TFile *hfile = new TFile("foo.root","READ");
> TDirectory *dir = (TDirectory *) hfile->Get("dir");
> dir->cd();
>
> TTree *tree = (TTree*) dir->Get("T");
>
> Int_t tmp;
> tree->SetBranchAddress("B",&tmp);
> tree->GetEvent(0);
> printf("i tmp = %d %d\n",i,tmp);
>
> delete tree;
> delete dir;
> hfile->Close();
> delete hfile;
> }
> }