Double deletion of objects with TRint->Run(true)

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;


	// let the call return

	// 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! :slight_smile:

Martijn (8.93 KB)

When the application quits, several ROOT-bookeeped lists are deleted including the objects in these lists, in particular
-list of files
-list of canvases

see chapter about Object Ownership in the Users guide.
In your case the best solution is to create your objects in the heap.



thanks for your quick reply :slight_smile: !

So I should create the objects on the heap and not delete them, if they are derived from TCanvas or are drawn to a TCanvas? This means that I should only delete the Objects where I am sure that ROOT does not delete them automatically (so I have to keep track of that).

But why does ROOT delete the objects when they are not on the heap? If you let the Run method return you will always get segmentation faults, then.


In general it is not a good idea :
-to create your own main program
-to create stack objects inside the main program

None of the problems that you are reporting would exist if you were using the standard root.exe.