Double free or corruption error at end of macro

I am reliably able to crash root with my macro at the very end of its execution with such an error:

*** glibc detected *** /home/wam/src/root/bin/root.exe: double free or corruption (!prev): 0x0a1261c0 ***

In this macro I read and fit some histograms from a file using:

TH1F *avgasy = (TH1F*)f->GetObjectChecked("Avg","TH1F"); 

TH1F *scans[numscans];

char scanname[100];

for (int i=0;i<numscans;i++) {
 sprintf(scanname,"Asymmetry for scan %d",i+1);
 scans[i+1]=(TH1F*)f->GetObjectChecked(scanname,"TH1F");
}

I open several canvases and make some plots, and it all works fine until the execution reaches the end of the macro where it crashes with

I suspect it may be a memory management issue, but nothing is obvious to me.

valgrind does find some memory loss from the heap (with a g++ compiled version of the macro)

Thanks for any suggestions!

Andrew

[quote=“wamlet”]

TH1F scans[numscans];

for (int i=0;i<numscans;i++) {

scans[i+1]=(TH1F
)f->GetObjectChecked(scanname,“TH1F”);//**

}
[/code]

** what is this ‘i + 1’? What is the value of i + 1 when i == numscans - 1 (the last iteration) ?
Do you really need valgrind?

you are right! foiled by indexing… thanks