Hi Rob,
Only Histogram, TTree and TEventList are owned (by default) by the TFile
object.
In your case that means that once you do:
TFolder* obj = (TFolder*)key->ReadObj(); // This line cause seg vio
You 'own' the TFolder and you should delete it when your done with it.
So you need to finish your function with:
delete obj;
to avoid the memory leak.
Cheers,
Philippe.
PS. Note that in ROOT 4.00/08, instead of
TKey *key = f->GetKey(k);
TFolder* obj = (TFolder*)key->ReadObj();
you can do
TKey *key = f->GetKey(k);
TFolder* obj = (TFolder*)key->ReadObj(TFolder::Class());
where obj will be set to NULL if there is an object with
the requested name but this object is not a TFolder.
Better yet, you could use:
TFolder *obj; f->GetObject(k,obj);
where obj will be set to NULL if there is an object with
the requested name but this object is not a TFolder.
-----Original Message-----
From: owner-about-root_at_listserv.fnal.gov
[mailto:owner-about-root_at_listserv.fnal.gov] On Behalf Of Rob Snihur
Sent: Thursday, March 17, 2005 9:03 PM
To: about-root_at_fnal.gov; roottalk_at_root.cern.ch
Subject: ReadObj memory leak?
Hi,
I am encountering a potential memory leak
associated with the ReadObj command.
Briefly, I am attempting to open a file
which contains histograms, and access
the pointer to a particular histogram.
The file structure contains a key which
is a TFolder (called "Ana") at the top level,
and contains many sub-folders,
ultimately containing histograms.
The histogram file (~2 MB) and root macros are available at:
http://www-cdf.fnal.gov/~snihur/root_leak/
or directly at
fcdflnx4.fnal.gov:/cdf/home/snihur/public_html/root_leak
I show two simple macros below (m1 calls m2)
which illustrate the problem. As I iterate,
I can see memory consumption growing
(with the "top" command),
and I finally get a memory error.
The only time I allocate memory,
I believe that I delete it as well.
Where is the memory leak?
I am using root 4.00/08 in the CDF environment
(what I get after I do
"source ~cdfsoft/cdf2.cshrc; setup cdfsoft2 development").
I also see a similar error in root 3.05/07
(under "setup cdfsoft2 5.3.3").
thanks,
-rob
fcdflnx4 501% cd public_html/root_leak/
/cdf/home/snihur/public_html/root_leak
fcdflnx4 502% cat m1.C
#include "m2.C";
void m1(int istart=0, int iend=10)
{
for(int i=istart;i<iend;i++) {
cout << "i=" << i << endl;
Int_t h =
m2("test.root","JetProbBlockGenpBlock0/Hist/JetProbBlockGenpBlock0_JppPos","
Ana");
}
}
fcdflnx4 503% cat m2.C
#include <iostream.h>
#include <TFile.h>
#include <TH1.h>
#include <TFolder.h>
#include <TKey.h>
#include <TObject.h>
Int_t m2(const Char_t *fname, const Char_t *hname, const Char_t *k)
{
TH1::AddDirectory(kFALSE);
TFile *f = new TFile(fname); // Open file on heap.
if ( f == 0 ) {
cout << "Failed" << endl;
return 0;
}
TKey *key = f->GetKey(k);
TFolder* obj = (TFolder*)key->ReadObj(); // This line cause seg vio
//TObject* obj1 = obj->FindObject(hname);
//TH1F *h = (TH1F*)obj1;
if ( f != NULL ) delete f;
return 0;
}
fcdflnx4 504% setup cdfsoft2 development
fcdflnx4 505% which root
/home/cdfsoft/products/root/v4_00_08gGCC_3_4_3/Linux+2.4/bin/root
fcdflnx4 506% root
FreeType Engine v2.1.3 used to render TrueType fonts. Compiled for linux with thread support.
CINT/ROOT C/C++ Interpreter version 5.15.138, May 23 2004
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] TFile f("test.root");
root [1] f.ls();
TFile** test.root TFile* test.root
i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8
This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:06 MET