Proper deletion of TTree and TFile

Hi,

I am attaching a small macro that creates a root-tuple, fills it with data and saves it in a root file. My questions are:

  1. Does file “output_file” own the “root_tree” tree in my example?
  2. If yes: will deleting output_file, also delete root_tree (i.e. is there a memory leak in my macro or not)?

Thanks!

PS. which root
/afs/cern.ch/cms/external/lcg/external/root/5.08.00/slc3_ia32_gcc323/root//bin/root

// root stuff
#include <TROOT.h>
#include <TChain.h>
#include <TFile.h>
#include <TLeaf.h>
#include <TBranch.h>
#include <TRandom.h>

// maximum # of tracks in event
const int N_MAX = 50;

/// class containing output root-tuple structure
class Event : public TObject {
public: 
  /// number of tracks
  Int_t N;
  /// event #
  Int_t evt_no;
  // px, py
  Float_t * px; // [N]
  Float_t * py; // [N]
  
  // constructor
  Event(void){
    px = new Float_t[N_MAX];
    py = new Float_t[N_MAX];
  }
  
  // destructor
  ~Event(void){
    delete [] px; delete [] py;
  }

  ClassDef(Event, 1)
};

int variableLengthArray(void)
{
  
  TFile * output_file = new TFile("temp.root", "recreate");
  TTree * root_tree = new TTree("tree", "root-tuple description");
  root_tree->SetDirectory(output_file);
  
  Event * evt = new Event();
  TBranch * branch = root_tree->Branch("myTuple", "Event", &evt, 8000, 2);

  for(int i = 0; i != 100000; ++i)
    { // loop over events

      // get # of tracks for event
      evt->N = int(gRandom->Gaus(10, 2) ); // gauss with mean = 10, sigma = 2

      // do not go beyond maximum length!
      if(evt->N > N_MAX)evt->N = N_MAX;

      evt->evt_no = i;

      for(int j = 0; j < evt->N; ++j)
   	{ // loop over tracks in event
	     evt->px[j] = gRandom->Gaus(0, 5); // gauss with mean = 0, sigma = 5
        evt->py[j] = gRandom->Gaus(3, 2); // gauss with mean = 3, sigma = 2
	   } // loop over tracks in event

      root_tree->Fill();
      if(i % 50000 == 0)
	   {
	     cout << " Autosaving root tree for event #: " << i << endl;
	     root_tree->AutoSave();
	   }

    } // loop over events
  
  output_file->cd();
  root_tree->Write();
  output_file->Close();
  
  delete evt;
  //  delete root_tree; // *** causes segmentation fault !
  delete output_file;

  return 0;

}

I strongly suggest you read the short chapter about "Object Ownership"
in the Users Guide.

[quote]1. Does file “output_file” own the “root_tree” tree in my example?
[/quote]
yes it does. Note that you can remove the line
root_tree->SetDirectory(output_file);
As explained in the Guide, if you do not want the current directory
to be the owner of your Trees, histograms, call
TH1::AddDirectory(kFALSE);
before the creation of your objects.

[quote]2. If yes: will deleting output_file, also delete root_tree
(i.e. is there a memory leak in my macro or not)?
[/quote]
deleting the file will automatically delete all the objects
owned by this file, including your Tree.
Note that you can simplify your logic.
Instead of:

[code] output_file->cd();
root_tree->Write();
output_file->Close();

delete evt;
// delete root_tree; // *** causes segmentation fault !
delete output_file;
[/code]
you just need:

root_tree->AutoSave(); delete output_file;

Rene