RE: [ROOT] Segfault while iterating over file

From: Valeri Fine (fine@bnl.gov)
Date: Fri Sep 06 2002 - 22:07:32 MEST


> 
> To ietrate on all keys of a Root file, use a normal iterator, eg:

It is what class TFileIter does exactly. 

See:
http://root.cern.ch/root/htmldoc/src/TFileIter.cxx.html#TFileIter:ReadOb
j

Using "normal iterator" should not help. It should crash also. One needs
to see that ROOT file and the ROOT version to understand the reason of
the troubles.

  Thank you, Valeri 


> 
> void iloop() {
>    TFile f("hsimple.root");
>    TIter nextkey(f.GetListOfKeys());
>    TKey *key;
>    while ((key=(TKey*)nextkey())) {
>       TObject *obj = key->ReadObj();
>       printf("at key:%s, object
> class:%s\n",key->GetName(),obj->ClassName());
>       delete obj;
>    }
> }
> 
> Rene Brun
> 
> On Fri, 6 Sep 2002, Jeroen Hegeman wrote:
> 
> > Yes, I first tried 'delete currentObject', 'currentObject->Delete()'
was
> > an attempt to solve the problem.
> > Actually, the problem is in the GetObject() call, even after
removing
> all
> > the delete lines, it still segfaults in that line (in the third run
> after
> > starting ROOT, in the second iteration, to be precise).
> >
> > And it looked like such a simple task :(
> >
> > Jeroen
> >
> > On Thu, 5 Sep 2002, Valeri Fine wrote:
> >
> > > Hello I wonder if your problem came from the statement
> > >
> > > >     currentObject->Delete();
> > >
> > > This implies the current object is in possesion of the "proper"
method
> > > "Delete()"
> > > One needs very strong reason and solid knowledge to use this
method.
> > >
> > > Did you try
> > >
> > >        delete currentObject;
> > >
> > > instead  as it is suggested by
> > >
http://root.cern.ch/root/htmldoc/TFileIter.html#TFileIter:description
> ?
> > >
> > > On other hand you may not test
> > >
> > > >   if (currentObject) {
> > > >     currentObject->Delete();
> > >
> > > because TFileIter  checks this itself.
> > >
> > > Just "delete currentObject;" should be sufficient.
> > >
> > > But . . .
> > >
> > > I'd like to mention one should not try to "delete" the histogram
> objects
> > > from the "read-only" file.
> > > This may cause you some extra troubles as well. Such kind of the
> object will
> > > be deleted
> > > automatically just TFile dtor is called
> > >
> > > "deleting" the histogram object read from the "writable" TFile
would
> delete
> > > the object from the file.
> > > So if your object is derived from TH1 class then  leave it alone,
> don't try
> > > to delete it.
> > > (see:
> http://root.cern.ch/root/htmldoc/TObject.html#TObject:InheritsFrom )
> > >
> > > Hope this helps,
> > >                  Cheers, Valeri
> > >
> > >
> > > ----- Original Message -----
> > > From: "Jeroen Hegeman" <jhegeman@nikhef.nl>
> > > To: <roottalk@pcroot.cern.ch>
> > > Sent: Thursday, September 05, 2002 10:38 AM
> > > Subject: [ROOT] Segfault while iterating over file
> > >
> > >
> > > > Hi everyone,
> > > >
> > > > While trying to loop over all objects in a .root file (in order
to
> dump
> > > > all
> > > > histograms at once), I seem to have an unexspected problem. The
> below code
> > > > results in a 'segmentation fault' every second or third run.
> > > > The problem has nothing to do with the assignment to
currentObject
> because
> > > > the commented line in the code does the same (apart from
> leaking...).
> > > > Replacing GetObject() with GetKeyName() gives no problems, the
> iterator is
> > > > fine and when it does run, the values that come out are right.
> > > > I have looked through roottalks and tried debugging but I must
be
> missing
> > > > something, anybody have a clue?
> > > >
> > > > Thanks in advance,
> > > > Jeroen
> > > >
> > > > {
> > > > // Load the library we need for TFileIter
> > > > gSystem->Load("libStar");
> > > >
> > > > TObject* currentObject = NULL;
> > > > TFile* inFile = new TFile("hsimple.root","READ");
> > > > TFileIter* iter = new TFileIter(inFile);
> > > >
> > > > Int_t nrOfObjects = iter->TotalKeys();
> > > >
> > > > // Loop over all objects in inFile.
> > > > for (iter->Reset(); (int)(*iter) < nrOfObjects;
iter->SkipObjects())
> {
> > > >   cout << "\ngoing to extract object #" << (int)(*iter) << "\n";
> > > >
> > > >   //  cout << iter->GetObject() << "\n";
> > > >   if (currentObject = (iter->GetObject())) {
> > > >     cout << "current object extracted\n";
> > > >   };
> > > >
> > > >   if (currentObject) {
> > > >     currentObject->Delete();
> > > >   };
> > > > }; // (End of object-loop.)
> > > >
> > > > if (inFile->IsOpen()) {
> > > >   inFile->Close();
> > > > };
> > > > delete inFile;
> > > > };
> > > >
> > > >
> > >
> > >
> >



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