RE: [ROOT] Segmentation fault. Cause: Closing a TFile

From: Philippe Canal (pcanal@fnal.gov)
Date: Wed Jul 02 2003 - 19:05:39 MEST


Hi,

> In my code, tbuf is appended to myfile, but not ctbuf, since I call
> ctdir->cd() I before create it. The TFile associated with ctbuf is
> supposed to be the one that contains ctdir.

Indeed, this seems correct (my mistake).  Then the other possibility is the following.  Since you do not set the addresses for the tree tbuf, when you do CopyTree, tbuf allocates some memory (to hold the value that will be read) and shared this memory with the new tree BUT tbuf keeps ownership of the memory and thus tbuf deletes this memory when it is deleted.  Hence later on when you try ctbuf->Draw, ctbuf tries to use memory that has already been deleted.  You have 3 possibles work around
	a) make calls to tbuf->SetAddress("xxx",ptr) before copying it.
	b) make calls to ctbuf->SetAddress("xxx",0) after the copy
	c) save, close and reopen fluxdir (this will reset the address for ctbuf).

Cheers,
Philippe.

-----Original Message-----
From: Pierre-Luc Drouin [mailto:pldrouin@physics.carleton.ca]
Sent: Wednesday, July 02, 2003 11:50 AM
To: Philippe Canal
Cc: roottalk@pcroot.cern.ch
Subject: RE: [ROOT] Segmentation fault. Cause: Closing a TFile


Hi,

In my code, tbuf is appended to myfile, but not ctbuf, since I call
ctdir->cd() I before create it. The TFile associated with ctbuf is
supposed to be the one that contains ctdir.

Thank you

Pierre-Luc Drouin

On Wed, 2 Jul 2003, Philippe Canal wrote:

> Hi Pierre-Luc,
> 
> When you close a file, all the objects 'Appended' to this file are deleted.
> In particular, histogram and trees are automatically appended to the current
> directory.  Hence, 'myfile->Close();' as the side-effect of deleting ctbuf.
> 
> You might want to (re)read the chapter on object ownership in the ROOT Users
> Guide.
> 
> Note that your code is missing (at least in this copy/pasted version) and
> essential
> 	myfile->Write();
> without which the tree is not fully written/flushed to the file.
> 
> Cheers,
> Philippe
> 
> -----Original Message-----
> From: owner-roottalk@pcroot.cern.ch
> [mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Pierre-Luc Drouin
> Sent: Wednesday, July 02, 2003 11:03 AM
> To: roottalk@pcroot.cern.ch
> Subject: [ROOT] Segmentation fault. Cause: Closing a TFile
> 
> 
> Hello,
> 
> I've to open a TFile, get a TTree instance from it, and copy this TTree
> with some selections to a new TTree object and then to close the TFile. My
> code is the following:
> 
> 	    TTree *tbuf, *ctbuf;
>             TDirectory* fluxdir= \\A pointer to a TDirectory in a TFile
>             TDirectory *ctdir;
> 
> 	    TFile *myfile=new TFile("myfile.root","READ");
> 
> 
>           tbuf=dynamic_cast<TTree*>(myfile->Get("mytree"));
> 
> 	    ctdir=fluxdir->mkdir("Event Info");
> 	    ctdir->cd();
> 
> 	    ctbuf=tbuf->CopyTree(allcuts);
> 
> 	    myfile->Close();
> 	    gROOT->Delete(cardbuf[i][filenameindex]);
> 
> 
> I get a segmentation fault when I try to ctbuf->Draw() further in the
> code. Since gDirectory==ctdir in the code, I don't understand how ctbuf
> could depend on myfile. When I comment the last two lines in the code,
> everything works fine
> 
> Thank you
> 
> Pierre-Luc Drouin
> 



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:13 MET