Hi,
What you do is correct.
In your WriteMirrorEvent function, you should not delete fTree.
You should not delete fTree either in your class destructor.
TFile::close takes care of deleting all objects associated with this file.
However TFile::Close cannot reset fTree to 0. Setting fTree=0 after TFile::Close
is correct. You can remove the statement
if ( fTree ) delete fTree;
from your class destructor.
Concerning your comment in WriteMirrorEvent: TTree::Branch has to build
a temporary object of your class to create the corresponding class dictionary.
The default constructor of the class is called, then the object is deleted.
Rene Brun
mathes@ik3.fzk.de wrote:
>
> Hello Rooters,
>
> I have a question concerning the ownership of some ROOT objects.
> In the below attached example I have a class derived from TFile which
> contains a TBranch and a TTree object. The class is used either for
> reading or for writing.
>
> Do I have to release the storage for the TTree object in the case of
> TMirrorEventFile::WriteMirrorEvent() ?
> Obviously not, because I get a SEGV when doing delete fTree. But if this
> is handled somewhere internally then delete fTree should do nothing ?
>
> I am using Root 2.24/05 (root_v2.24.Linux.2.2.14).
>
> Thanks
> Hermann-Josef
>
> -----------------------------------------------------------------------
>
> #include <stdlib.h>
> #include <stream.h>
> #include <TFile.h>
> #include <TTree.h>
>
> #include "MiEventFile.hh"
>
> TMirrorEventFile::TMirrorEventFile() : TFile("data.root"),
> fBranch(0),
> fEvent(0),
> fFileName("data.root"),
> fTree(0) {}
>
> TMirrorEventFile::TMirrorEventFile( const char *fname, Option_t *option="",
> const char *ftitle="", Int_t compress=1 ) :
> TFile(fname, option, ftitle, compress),
> fBranch(0),
> fEvent(0),
> fFileName(fname),
> fTree(0) {}
>
> TMirrorEventFile::~TMirrorEventFile()
> {
> // Obviously the TFile is the owner of these objects
>
> if ( fTree ) delete fTree;
> }
>
> void TMirrorEventFile::Close( Option_t *option="" )
> {
> // file was opened in write mode ...
>
> if ( TFile::GetBytesWritten() ) {
> TFile::Write();
>
> // fTree = 0;
> fBranch = 0;
> }
>
> // cout << TFile::GetBytesWritten() << endl;
> // cout << TFile::GetBytesRead() << endl;
>
> // file was opened in read mode ...
>
> if ( TFile::GetBytesRead() ) {
> fTree = 0;
> fBranch = 0;
> }
>
> TFile::Close( option );
>
> fEvent = 0; // now this address is invalid
> }
>
> Bool_t TMirrorEventFile::ReadMirrorEvent( TMirrorEvent** event )
> {
> Bool_t valid_event = kFALSE, file_end = kFALSE;
>
> if ( !fTree ) {
> fTree = (TTree *)fFile->Get( "EventTree" );
> if ( !fTree ) {
> cerr << "No Tree with given name found !" << endl;
> return kFALSE;
> }
>
> fNentries = (Int_t)fTree->GetEntries();
> }
>
> fBranch = fTree->GetBranch( "EventBranch" );
> if ( !fBranch ) {
> cerr << "No branch with name 'EventBranch' found !" << endl;
> return kFALSE;
> }
>
> while ( fBranch && !valid_event && !file_end ) {
> fBranch->SetAddress( event );
>
> if ( fCurrentEvent < fNentries ) {
> valid_event = kTRUE;
>
> fTree->GetEvent( fCurrentEvent );
> fCurrentEvent++;
> }
> else {
> cout << "End of file reached !" << endl;
>
> file_end = kTRUE;
> }
> }
>
> if ( valid_event )
> return kTRUE;
> else
> return kFALSE;
> }
>
> Bool_t TMirrorEventFile::WriteMirrorEvent( TMirrorEvent** event )
> {
> if ( !fTree ) {
> fEvent = *event;
>
> fTree = new TTree( "EventTree", "simple save tree" );
> if ( !fTree ) {
> cerr << "Could not create Tree !" << endl;
> return kFALSE;
> }
>
> fTree->SetAutoSave( 1000000 ); // autosave when 1M written
>
> // create branch not in split mode !
> // Note: this calls internally TMirrorEvent() and ~TMirrorEvent()
> // which is not quite clear to me ...
>
> fTree->Branch( "EventBranch", "TMirrorEvent", event, 64000, 0 );
> }
>
> if ( *event != fEvent ) {
> cerr << "Changed address of data buffer !" << endl;
> return kFALSE;
> // fTree->Branch( "EventBranch", "TMirrorEvent", event, 64000, 0 );
> }
>
> fTree->Fill();
>
> return kTRUE;
> }
>
> **************************************************************************
> * *
> * Dr. Hermann-Josef Mathes *
> * KASCADE & AUGER Collaboration *
> * Forschungszentrum Karlsruhe Phone: +49 7247 822429 *
> * Institut fuer Kernphysik FAX: +49 7247 824075 *
> * POB 3640 *
> * D-76021 Karlsruhe/Germany Mail: mathes@ik3.fzk.de *
> * hjmathes@web.de *
> * *
> **************************************************************************
> * Feel free to visit my homepage: *
> * http://www-ik3.fzk.de/~mathes/Welcome.html *
> * My software project page for DAQ: *
> * http://www-ik3.fzk.de/~mathes/software/software.html *
> * *
> * Visit the institute and project homepages: *
> * http://www-ik3.fzk.de/ *
> * http://www-ik1.fzk.de/KASCADE_home.html *
> **************************************************************************
This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:34 MET