TCanvas::Constructor:0: RuntimeWarning: Deleting canvas with same name:

This short python script:

#! /usr/bin/env python                                                                                                                                                                                               
import ROOT

c1 = ROOT.TCanvas('c', 'c')
c2 = ROOT.TCanvas('c', 'c')

generates a warning:

TCanvas::constructor:0: RuntimeWarning: Deleting canvas with same name:

I tried to call TCanvas::Delete()

which generates a warning:

TCanvas::Delete():0: RuntimeWarning: may not use this method

How to solve this problem, given that the canvas name ā€˜cā€™ is fixed?

Interestingly, there is no problem with canvas names in the c++ equivalent:

#include "TCanvas.h"
int main()
  TCanvas c1("c", "c");
  //  c.Delete();                                                                                                                                                                                                    
  TCanvas c2("c", "c");

Where is there a difference between python and c++?

Hi @Viesturs,

I believe the difference is due to this line:

The C++ object that PyROOT wraps is allocated on the heap, while from C++ you are allocating that object on the stack. If you create your object with new in C++ you will see the same warning.

The will be no leak, it is just ROOT warning you that it will destroy the other canvas.


1 Like

Indeed. I would not have imagined that allocating on stack or on heap would cause any functional difference.

Then how to solve? TCanvas::Close()?

@couet any advice on this?

@Viesturs you are worried about the warning? It should not interfere with what you want to do in the end.

Creating two different canvases with the same name is not a good idea anyway as ROOT tracks the objects in memory . You are just making things confusing. Use different names and it should be fine.

@couet, in the real situation canvases are created in a loop. Should I call TCanvas::Close() on the canvas from the previous iteration?

Even if you create the canvases in a loop you can specify different names.
Imagine ā€œiā€ is the index of the loop. In C++ you can do:

TCanvas c1(Form("c%d",i), Form("c%d",i));
1 Like

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