Hey ROOTers!
I ran into a problem recently, which occurs when you want the Run method of TApplication or better the TRint to return. If you now close the canvas (by clicking on the x on the top right of the window) and then close the interactive application by typing “.q”, the destructors of the drawn objects on the canvas get called twice!
I am running ROOT 5.26/00 on Windows and I am using Visual Studio C++ 2009 Express Edition.
Because I am using also non-ROOT based objects in my code which require explicit cleanup, the simplified structure of my program goes like this:
#include <TMath.h>
#include <TGraph.h>
#include <TRint.h>
#include "MyCanvas.h"
int main(int argc, char **argv)
TRint theApp("ROOT example", &argc, argv);
// create some non-root objects
// generate some data
Int_t n = 20;
Double_t x[20], y[20];
for (Int_t i = 0; i < n; i++)
{
x[i] = i * 0.1;
y[i] = 10 * TMath::Sin(x[i] + 0.2);
}
TGraph myGr(n, x, y);
MyCanvas myC;
myGr.Draw("AC*");
// let the call return
theApp.Run(true);
// some cleanup of non-root objects
return 0;
}
(Please find a zip archive containing the code with the Visual Studio project in the attachment)
When the Run method returns and the program reaches the return statement, the destructor of the TGraph myGr is called. But it was called already when closing the canvas. This behaviour is also occurs, when you create the objects on the heap and explicitly delete them after the TRint returns (see the heap version of the main function provided in the archive).
I especially wonder why the destructors get called even though the objects are not on the heap? Is this a maybe a bug?
Also, with the version on the heap it would be a bit unhandy to remember which objects are deleted by root (i.e. are based on root and drawn on the canvas) and which are not based on root and have to be deleted by hand.
Thanks for taking the time and I am looking forward to your replies!
Cheers,
Martijn
test.zip (8.93 KB)