ReadObj memory leak?

From: Rob Snihur <snihur_at_fnal.gov>
Date: Thu, 17 Mar 2005 21:03:12 -0600


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

  KEY: TFolder Ana;1 StnAna folder
root [2] .q
fcdflnx4 507% root -l
root [0] .x m1.C(1,10)
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8

terminate called after throwing an instance of 'std::bad_alloc'   what(): St9bad_alloc Received on Fri Mar 18 2005 - 04:03:17 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:06 MET