This short python script:
#! /usr/bin/env python
import ROOT
c1 = ROOT.TCanvas('c', 'c')
#c1.Delete()
c2 = ROOT.TCanvas('c', 'c')
generates a warning:
TCanvas: 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:
SetBit(kMenuBar,1);
if (form < 0) {
form = -form;
SetBit(kMenuBar,0);
}
fCanvas = this;
fCanvasID = -1;
TCanvas *old = (TCanvas*)gROOT->GetListOfCanvases()->FindObject(name);
if (old && old->IsOnHeap()) {
Warning("Constructor","Deleting canvas with same name: %s",name);
delete old;
}
if (!name[0] || gROOT->IsBatch()) { //We are in Batch mode
fWindowTopX = fWindowTopY = 0;
if (form == 1) {
fWindowWidth = gStyle->GetCanvasDefW();
fWindowHeight = gStyle->GetCanvasDefH();
} else {
fWindowWidth = 500;
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.
Cheers,
Enric
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.
couet
July 30, 2018, 8:12am
5
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?
couet
July 30, 2018, 8:42am
7
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
system
Closed
August 13, 2018, 8:56am
8
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.