Plotting many TGraphs, memory problem

I have a problem that’s probably not about ROOT but rather about memory in C++, but I thought posting here would be a good idea anyway. I want to plot many curves in the same canvas, my function looks like this:

[code]void printCurves(TH3D* onePart3Dhist, TH3D* twoPart3Dhist) {
/*
for each bin (layer,etamod):
Calculate integrals as usual.
Print onto canvas.
*/
gPad->SetLogx();
int binxlimit=onePart3Dhist->GetXaxis()->GetNbins();
int binylimit=onePart3Dhist->GetYaxis()->GetNbins();

for (int i(1); i<=binxlimit; i++) {
	for (int j(1); j<=binylimit; j++) {
		TH1D* projection1P=onePart3Dhist->ProjectionZ("", i, i, j, j);
		TH1D* projection2P=twoPart3Dhist->ProjectionZ("", i, i, j, j);
		double* integral1=projection1P->GetIntegral();
		double* integral2=projection2P->GetIntegral();
		double integral4[1000]; 
		for(int u(0); u<1000; u++) {
			integral4[u]=1-integral2[u]; 
		}
		TGraphErrors *graph=new TGraphErrors(1000, integral1, integral4,NULL,NULL);
		if(j==1) {graph->Draw("AP");}
		else { graph->Draw("P");}

// delete graph; delete projection1P; delete projection2P;
}
TString pn(""); pn+=i;
canvas->SaveAs(“PerformanceCurvesLayer”+pn+".png");
}
}[/code]

First of all, I’m wondering about the “delete”'s which I commented out. I thought they should be there, but they produce a seg fault. Valgrind is pointing toward the deletes complaining about read error of size 8. Further, I still just get one curve saved in my png:s, which all look the same, and sometime (with slightly modified code, I forget exactly what), I got “Error: + Illegal operator for pointer 1” pointing toward the Draw(). I’m guessing all of this must have to do with me treating memory wrong somehow.

You should not delete the graphs of course… or they will be missing at the painting time.
The way you wrote the loop make me think you want to have all the graphs on the same plot
In that case should do the SaveAs outside the loop.

OK, but what’s the reason the histos should not be deleted? I thought whenever you declared something with “new” you have to delete it, in case you want to redeclare it. I’m also curious to why this works at all, for example, integral1 is a pointer, so when it’s redeclared, the former graph should lose its information? What’s actually stored in the graph, and what’s stored in the canvas?

I want to save the canvas for each y bin, so I think that one is right. (By the way, what’s the difference between canvas->SaveAs and canvas->Print?) I got this to work now, by adding some name in ->ProjectionZ(“name”) instead of an empty string “”. Isn’t that strange, that this makes a difference?

May be you can post a running example so we can see what’s going wrong.

Here’s a similar problem I have now. I attach the script and the root file needed.
makeplot.C (3.52 KB)
perf.root (116 KB)

yes, you should not delete the graphs other wise the Legend get lost etc …

Could you explain a bit more why that’s wrong? I mean, I’m plotting the graphs and the legend before deleting the graphs. Why is this an exception to the custom of deleting allocated memory?

The graphs and legends are part of the canvas (you can edit it and grow and shrink it). And the canvas is still there. If you delete the graphs then the canvas and the legend in the canvas who are both referring to the graphs get lost.