Canvas deletion

I’m currently using a compiled ROOT program in which canvases are deleted dynamically.
However, the program crashes if a pointer to a canvas that is to be deleted has already been “closed” by the user. What should be the right way to check that a pointer to a canvas is still valid ?

Thanks a lot.

You can check if your pointer is still registered to the list of canvases,
eg if you have TCanvas *c1,

if (gROOT->GetListOfCanvases()->FindObject(c1)) { //ok c1 is a valid canvas }

Thanks a lot Rene !

  1. TCanvas* c = new TCanvas(“cName”,“cTitle”,0,0,400,400);

  2. if (gROOT->GetListOfCanvases()->FindObject©) cout << " found " << endl; else cout << " NOT found " << endl;

output is “found” - which is just fine

canvas deleted at this point by killing window
and line 2 executed again
and instead of " NOT found" I see this:

Error: Symbol c is not defined in current scope (tmpfile):1:
*** Interpreter error recovered ***

You should not call FindObject©, but FindObject(cName).
When killing the canvas, the CINT object point by c is also deleted, so c is now an unknow variable.


that much I figured out :slight_smile:.

it does not seem to be possible to check whether variable exists or not before trying to use it, which seems to be needed in an environment where variables are deleted in a sneaky manner.

P.S. this is totally against C++ rules. Perhaps better to set pointer to NULL instead of deleting it ??? Then I just have to check whether it is NULL, which is totally in sync with C++.