[ROOT] TCanvas and memory trouble

From: Matthieu Guillo (guillo@jlab.org)
Date: Tue Jul 17 2001 - 04:36:43 MEST


Hello Rooters

I have some trouble managing memory with TCanvas objects.
I have an array of TCanvas that I dynamically allocate (the size is not
known at run time). So I do;

TCanvas** c1 = NULL;
.......................
// the size of the array is now known, it is "nHistos"

c1 = TCanvas*[nHistos];
for (int i = 0; i < nHistos; i++) {
    char  szPadName[10];
    sprintf(szPadName, "c%d", i + 1);
    c1[i] = new TCanvas(szPadName, szPadName, 10 + 20 *
i, 10 + 20 * i, 750 + 20 * i, 940 + 20 * i);
}
	
This works fine, I can use the canvas without trouble. But when I try to
 delete them and free the memory:

if (c1) {
  for (int i = 0; i < nHistos; i++) {
    if (c1[i]) {
      delete c1[i];
      c1[i] =  NULL;
    }
  }
  delete[] c1;
}

This works also fine, except after I have deleted a canvas using the mouse
on the x button. Then everything crashes. Obviously ROOT makes some 
housekeeping I don't know and all some of my pointers are now dangling. 

How can I solve this problem? I tried also to use the gROOT pointer and
the GetListOfCanvases() as in:

if (gROOT->GetListOfCanvases()->FindObject(c1[i]->GetName())) {
  delete c1[i];
} 

but this doesn't help since the problem seems to be with the pointer c[i]
itself.

Thanks a lot.

Matthieu Guillo
University of South Carolina
Thomas Jefferson National Laboratory
Office 71 trailer 16
Phone: 757-269-5551



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:52 MET