Re: Segmentation violation using TNamed

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Feb 14 2000 - 12:23:24 MET


Hi Laurent,
Objects like histograms, trees, geometry nodes are automatically added
to a list of objects in the current directory. This list is a THashList.
When the name of an object is modified, the hashtable of the THashList
must be recomputed. This is done automatically when TH1::SetName or
TTree::SetName or TNode::SetName is called. It should also be done
when the function SetObject is called. For the time being this function
was only defined in TNamed. I have now added the redefinition of
SetObject
in TH1, TTree and TNode to recompute automatically the hashtable.
I have modified your example below to add two lines to force the
recomputation
of the hashtable. These two statements will not be necessary anymore
in version 2.24.

Rene Brun

Laurent CONIN wrote:
> 
> Hi
> 
> I'm using TNamed ject (TH1D) in my code. I want to copy and rename them.
> 
> you can find below an  example of segmentation violation error I get.
> 
> I get an eror on both Linux (compile with g++) an HP (compile with aCC)
> 
> It Crash in first loop.
> 
> It pass first loop if i comment out first SetObject statement
> but it crash in second loop
> 
> It pass second loop if i comment out second SetObject statement
> 
> Is there a bug on SetObject or am i doing something wrong ?
> 
> Laurent Conin
> 
> Ps; my Root version is 2.22/10
> 
> #include <strstream.h>
> #include "TROOT.h"
> #include "TH1.h"
> int main(){
> 
>   TROOT HbtRoot("Test", "Test");
> 
>   TH1D* test=new TH1D("test","test",100,0.,1.);
>   test->SetObject("test_b","test_b");  // first SetObject
    THashList *list = (THashList*)gDirectory->GetList(); <<====new line
    list->Rehash(1000); <======= new line
    
>   test->Delete();
> 
>   cout << "first loop OK" << endl;
> 
>   TH1D **test2=new TH1D*[1000];
> 
>   strstream *str;
> 
>   int i;
> 
>   // first loop
>    for(i=0;i<1000;i++) {
>     str=new strstream;
>     *str << "test2_" << i ;
>     cout <<str->str()<< endl;
>      test2[i]=new TH1D(str->str(),str->str(),100,0.,1.);
>     *str << "_b";
>     test2[i]->SetObject(str->str(),str->str());  //Second SetObject
>      delete str;
      list->Rehash(1000);  <======new line

>    };
> 
>    //second loop
>    for(i=0;i<1000;i++) { cout << i << endl;test2[i]->Delete();}
>    delete [] test2;
> }



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:18 MET