Too many canvasses (despite deleting them)?

Hi,

In the following code, I create a TCanvas in the subroutine and then delete it, each time with its own different name (if I don’t do that, I cannot create the canvas in the main routine). However, after printing the main “canvas”, I have the sensation that the other 100 canvasses are still in memory somehow, although I deleted them. The screen is very slow, and I cannot zoom in the main-canvas. When I comment out the canvas lines in the subroutine, the main canvas works fine.

void TEST( int in_pixel );

int  Main()
{
        TH1D* h1 = new TH1D("h1", "MAIN", 100, 0., 100.0);
        for (int pixel = 0; pixel < 100; pixel++)
        {
                TEST(pixel);
                h1->Fill(50);
        }
        TCanvas* cv2 = new TCanvas("cv2", "", 1000, 500);
        h1->Draw();
}

void TEST(int in_pixel)
{
        TString cname("test_"); cname += in_pixel;
        TCanvas* canvas = new TCanvas(cname, "", 500, 500);
        delete canvas;
}

What do you mean by this? It should not be necessary to create all these canvases. The following should be sufficient:

int  Main()
{
        TH1D* h1 = new TH1D("h1", "MAIN", 100, 0., 100.0);
        for (int pixel = 0; pixel < 100; pixel++)
        {
                h1->Fill(50);
        }
        TCanvas* cv2 = new TCanvas("cv2", "", 1000, 500);
        h1->Draw();
}

Sorry, I didn’t want to copy the entire code. In the subroutine, I plot a histogram with contents depending on “in_pixel” and each time plot it on a different Canvas, which I then print out to a file.
Of course, I could have one global Canvas, and re-use that same one again and again. But still, I’d like to understand why deleting a canvas does not work (seems like a memery leak to me).

Ahh, sorry I misunderstood your intention.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

I simplified your macro to:

void  Main1()
{
   for (int pixel = 0; pixel < 100; pixel++)
   {
      TString cname("test_"); cname += pixel;
      TCanvas* canvas = new TCanvas(cname, "", 500, 500);
      delete canvas;
   }
   return;
}

I run the system activity on mac to spy the size of root.exe and I see that the memory usage increase after each of execution of the macro.