RE: [ROOT] Segfault while iterating over file

From: Valeri Fine (fine@bnl.gov)
Date: Fri Sep 06 2002 - 21:52:41 MEST


Hello Jeroen,
  I can not reproduce your problem. (Which version of ROOT do you use
and where is it come from?)

  I did a hsimple.root file with $ROOTSYS/tutorials/hsimple.C macro.
  I did copy/paste your example to create a ROOT macro to read that 
  file back.

As one can see no crash was discovered.

root.exe [0] .x test2Iter.C 

going to extract object #0
current object extracted

going to extract object #1
current object extracted

going to extract object #2
current object extracted

going to extract object #3
current object extracted
root.exe [1] .q


I did try to re-design your macro to make it simpler:

{
  // Load the library we need for TFileIter
  gSystem->Load("libStar");
  TObject* currentObject = NULL;
  TFileIter iter("hsimple.root");
  Int_t nrOfObjects = iter->TotalKeys();
  // Loop over all objects in inFile.
  for (; (int)iter < nrOfObjects; ++iter)
  {
     cout << "\ngoing to extract object #" << (int)(*iter) << "\n";
     if (currentObject = *iter) {
       cout << "current object \"" << currentObject->GetName() << "\"
extracted\
n";
     };
     delete currentObject;
  }; // (End of object-loop.)
};

It works with no problem.

root.exe [0] .x testIter.C 

going to extract object #140702496
current object "hprof" extracted

going to extract object #140808496
current object "hpx" extracted

going to extract object #141918008
current object "hpxpy" extracted

going to extract object #141891832
current object "ntuple" extracted
root.exe [1] .q

At this point I would suspect something is wrong with YOUR file.

My best regards, Valeri 


> -----Original Message-----
> From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]
> On Behalf Of Jeroen Hegeman
> Sent: Friday, September 06, 2002 4:38 AM
> To: roottalk@pcroot.cern.ch
> Subject: Re: [ROOT] Segfault while iterating over file
> 
> 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