Memory leak problem

Dear Rooters,

I seem to have a memory leak problem while opening .root Files in a loop. Just opening and closing with TFile is not a problems but getting the Canvas from the respective TFile I can see how the virtual and the real memory are increasing with each loop. The error I am getting is the following:

Symbol G__exception is not defined in current scope /users/schultz/macros/MAGICIIReflectorPSF.C:73:
Error: type G__exception not defined FILE:/users/schultz/macros/MAGICIIReflectorPSF.C LIN

Here is the code I am using:
The aim is to retrieve a 2D histogram from a Subpad of a Canvas which I want to draw together with the histograms of the other .root Files in one new Canvas in a special way. I will also attach one of those .root files.
Thanks for your help.

Cornelia

[code]#include
#include
#include “TPaveStats.h”
#include “TCanvas.h”
#include “TStyle.h”
#include “TLatex.h”
#include “TH2F.h”
#include “TCanvas.h”
#include “TFile.h”

using namespace std;
void MAGICIIReflectorPSF()
{
int position[17][17]={145,128,111,94,77,60,43,26,9,162,179,196,213,230,247,264,281,
146,129,112,95,78,61,44,27,10,163,180,197,214,231,248,265,282,
147,130,113,96,79,62,45,28,11,164,181,198,215,232,249,266,283,
148,131,114,97,80,63,46,29,12,165,182,199,216,233,250,267,284,
149,132,115,98,81,64,47,30,13,166,183,200,217,234,251,268,285,
150,133,116,99,82,65,48,31,14,167,184,201,218,235,252,269,286,
151,134,117,100,83,66,49,32,15,168,185,202,219,236,253,270,287,
152,135,118,101,84,67,50,33,16,169,186,203,220,237,254,271,288,
153,136,119,102,85,68,51,34,17,170,187,204,221,238,255,272,289,
144,127,110,93,76,59,42,25,8,161,178,195,212,229,246,263,280,
143,126,109,92,75,58,41,24,7,160,177,194,211,228,245,262,279,
142,125,108,91,74,57,40,23,6,159,176,193,210,227,244,261,278,
141,124,107,90,73,56,39,22,5,158,175,192,209,226,243,260,277,
140,123,106,89,72,55,38,21,4,157,174,191,208,225,242,259,276,
139,122,105,88,71,54,37,20,3,156,173,190,207,224,241,258,275,
138,121,104,87,70,53,36,19,2,155,172,189,206,223,240,257,274,
137,120,103,86,69,52,35,18,1,154,171,188,205,222,239,256,273};

TCanvas *c0 = new TCanvas(“c0”,“PSF of MAGIC Reflector”,800,800);
gStyle->SetOptStat(0);
c0->SetFillColor(0);
c0->SetBorderSize(0);
c0->Divide(17,17,0,0);

listFile=string(“Files.txt”);

ifstream fCheck(listFile.c_str() );
if( !fCheck ) {
cout <<“Error opening " << listFile << " for input. Creating it!” << endl;
system(“ls mirror???.root > Files.txt”);
}
FILE *fpdata=fopen(“Files.txt”,“r”);
char line[15],file[15], pos[4];
int posx,posy,x,y;
while (fgets(line, 16, fpdata) != NULL) {
sscanf(line, “%s”,file);
strncpy(pos,&file[6],4);
pos[4] = ‘\0’;

sscanf(pos,"%d %d",&posx,&posy);
x=posx;
y=posy;
if(x<0)
  {
x=TMath::Abs(x)+8;
  }
if(y<0)
  {
y=TMath::Abs(y)+8;
  } 
FILE *fp;

if ((fp=fopen(line,"r"))!=NULL) {
  TString *string=new TString(line,15);
  TFile *tfile= TFile::Open(*string,"R");
  TCanvas *c1 = ( TCanvas*)tfile->Get("Data presentation");
TCanvas *c2 =(TCanvas*)c1->GetPrimitive("Data presentation_4");
  TH2F *h2=(TH2F*)c2->FindObject("SBIG image cleaned and   background corrected");
  c0->cd(position[x][y]);
  h2->GetXaxis()->SetAxisColor(0);
  h2->GetXaxis()->SetLabelColor(0);
  h2->GetXaxis()->SetLabelSize(0);
  h2->GetXaxis()->SetNdivisions(0);
  h2->GetYaxis()->SetAxisColor(0);
  h2->GetYaxis()->SetLabelColor(0);
  h2->GetYaxis()->SetLabelSize(0);
  h2->GetYaxis()->SetNdivisions(0);
  h2->DrawCopy("col");
  c0->Update();
  tfile->Close("R");
  tfile->Clear();
  tfile->DeleteAll();
  delete h2;
  delete c2;
  
  tfile->Close("R");
  tfile->Clear();
  delete tfile;
  gSystem->ProcessEvents();
  c1->Clear();
  c1->Close();
  delete c1;
  delete string;
  fclose(fp);
}

}
}[/code]

You have something strange in your script

TCanvas *c1 = ( TCanvas*)tfile->Get("Data presentation"); TCanvas *c2 =(TCanvas*)c1->GetPrimitive("Data presentation_4");
the canvas c1 cannot contain another canvas!

When calling c1->Clear you remove the primitives from the canvas but you do not delete them,
so later on “delete c1” will effectively delete the canvas but not the
primitives, so you should simply remove the damaging statement “c1->Clear()”.

Rene

The canvas, I am getting from the TFile is divided into 12 pads.
So, if I want to get a specific part of this canvas, I should rather declare it as a pad instead of canvas?

yes

Rene

I tried changing c2 from canvas to a pad erasing also the wrong “Clear” commands. The result is, that the histograms are not drawn, and I stil get the same error message. Watching the memory, I can still see a constant increase until the error occurs.
Any other Idea?

Cornelia

Could you simplify your script to the case of one single file and post your script and the input file? otherwise it is hard to understand what you try to achieve.

Rene

Yes I can, but just to let you know: Opening one file works perfectly. However, I hwould like to open about 250 files, taking one specific histogram, drawing a copy of this histogram at a certain position in a canvas, deleting all the pointers, closing the File and ging on with the next one.
Even closing and opening all TFiles is working, but once I try to get the canvas the aboce mentioned error occurs after a certain amount of iterations

I had to tar the file because it is quit big.
So here the -hopefully- more simple version:

#include
#include
#include “TPaveStats.h”
#include “TCanvas.h”
#include “TStyle.h”
#include “TLatex.h”
#include “TH2F.h”
#include “TCanvas.h”
#include “TFile.h”

using namespace std;
void MAGICIIReflectorPSF()
{//These are the positions of the single Pads in Canvas c0
int position[17][17]={145,128,111,94,77,60,43,26,9,162,179,196,213,230,247,264,281,
146,129,112,95,78,61,44,27,10,163,180,197,214,231,248,265,282,
147,130,113,96,79,62,45,28,11,164,181,198,215,232,249,266,283,
148,131,114,97,80,63,46,29,12,165,182,199,216,233,250,267,284,
149,132,115,98,81,64,47,30,13,166,183,200,217,234,251,268,285,
150,133,116,99,82,65,48,31,14,167,184,201,218,235,252,269,286,
151,134,117,100,83,66,49,32,15,168,185,202,219,236,253,270,287,
152,135,118,101,84,67,50,33,16,169,186,203,220,237,254,271,288,
153,136,119,102,85,68,51,34,17,170,187,204,221,238,255,272,289,
144,127,110,93,76,59,42,25,8,161,178,195,212,229,246,263,280,
143,126,109,92,75,58,41,24,7,160,177,194,211,228,245,262,279,
142,125,108,91,74,57,40,23,6,159,176,193,210,227,244,261,278,
141,124,107,90,73,56,39,22,5,158,175,192,209,226,243,260,277,
140,123,106,89,72,55,38,21,4,157,174,191,208,225,242,259,276,
139,122,105,88,71,54,37,20,3,156,173,190,207,224,241,258,275,
138,121,104,87,70,53,36,19,2,155,172,189,206,223,240,257,274,
137,120,103,86,69,52,35,18,1,154,171,188,205,222,239,256,273};

//Dividing canvas into 289 pads
TCanvas *c0 = new TCanvas(“c0”,“PSF of MAGIC Reflector”,800,800);
gStyle->SetOptStat(0);
gStyle->SetOptTitle(0);
c0->SetFillColor(0);
c0->SetBorderSize(0);
c0->Divide(17,17,0,0)
//Opening file and getting histogram from subpad
TFile *tfile= TFile::Open(“mirror+0+1.root”,“R”);
TCanvas c1 = ( TCanvas)tfile->Get(“Data presentation”);
TCanvas c2 =(TCanvas)c1->GetPrimitive(“Data presentation_4”);
TH2F h2=(TH2F)c2->FindObject(“SBIG image cleaned and background corrected”);
//Drawing a copy of histgram at certain position in canvas
c0->cd(position[0][1]);//normally I would get the respective position from the file name, but I think this is not important right now
h2->GetXaxis()->SetAxisColor(0);
h2->GetXaxis()->SetLabelColor(0);
h2->GetXaxis()->SetLabelSize(0);
h2->GetXaxis()->SetNdivisions(0);
h2->GetYaxis()->SetAxisColor(0);
h2->GetYaxis()->SetLabelColor(0);
h2->GetYaxis()->SetLabelSize(0);
h2->GetYaxis()->SetNdivisions(0);
h2->DrawCopy(“col”);
c0->Update();
//Delete Pointer
delete h2;
delete c2;
delete c1;
tfile->Close(“R”);
delete tfile;
}

Could you post as attachments the exact code that you use to read one single file
and also the root file containing your canvas?

Rene

Sorry, I will do this as soon as possible. I just have a problem compressing the data file sufficiently

So here at least first the code. Still, my file is too big.

Cornelia
MAGICIIReflectorPSF.C (2.6 KB)

Hi,

Do you still have the same problem or were able to solve it? If you still have the problem, can you reproduce it when you compile your script with ACLiC? If it does, what is the complaint (if any) when running with valgrind?
If this does not help, can you provide the data file on which to run your example?

Cheers,
Philippe.