Re: [ROOT] trouble with TFile in compiled code

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Apr 08 2002 - 21:30:30 MEST


Hi Jean-Pierre,

This problem is an easy one.
When you execute
  Run a;
The Run constructor will execute correctly. However when
finishing the execution of the constructor, all objects created
on the stack inside are automatically deleted (normal C++ scoping
rules). Your file is deleted and also the associated Tree.
When you call later
  a.NEH();
the pointer fh points to a deleted object.

The solution is to replace
     TFile  f("essai.root");
by
    TFile *f = new TFile("essai.root")

Rene Brun

On Mon, 8 Apr 2002, Jean-Pierre Ernenwein wrote:

> Hello all
> 
> 
> I have a very simple class with a TFile and a TTree, I would like to
> use it to fill an histogram, but when I try to access this histogramm, I
> have
> segmentation violation (call to NEH() method) :
> 
> class Run : public TNamed 
> {
>  private:
>   TH1F     *fh;
>  public:
>   Run()
>   {
>     TFile  f("essai.root");
>     TTree* t=(TTree*)f.Get("essait"); 
>     Float_t x;
>     t->SetBranchAddress("X",&x);
>     fh=new TH1F("essai","essai",100,0.,1.);
>     for (Int_t i=0; i<t->GetEntries();i++) 
>     {            
>           t->GetEntry(i); fh->Fill(x);	      	 
>     }  
>     cout <<  fh->GetEntries() << endl;   
>   };    
>   virtual ~Run();  
>   Stat_t NEH(){return fh->GetEntries();};
>  
>   ClassDef(Run,1)
> }; 
> 
> 
> in root I have :
> 
> root [0] gSystem->Load("libess.so");
> root [1] Run a;                     
> 100
> root [2] a.NEH();                   
> <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
> Error: Symbol nan is not defined in current scope  FILE: LINE:0
> *** Interpreter error recovered ***
> 
>  *** Break *** segmentation violation
> 
> 
> (I don't ask for a plot)
> the problem is similar in all last root versions
> 
> has someone an answer
> 
> BR
> 
> 
> 
>            Jean-Pierre Ernenwein, GRPHE
> 	   Universite de Haute Alsace, IUT Mulhouse, dept SGM / IReS Strasbourg
> 	   Tel: 03 89 33 75 56 (IUT) / 03 88 10 66 19 ou 66 77 (IReS) 
>            Portable : 06 89 26 40 46
> 



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:48 MET