Valgrind TGraphErrors constructor and Minuit in TF1::Fit()

Dear all,

//test.cc
#include <stdio.h>
#include <stdlib.h>
#include "TGraphErrors.h"
#include "TF1.h"
#include "TVectorD.h"
#include "TCanvas.h"

int main(int argc, char* argv[])
{
    const int ndata  = 5 ; 

    const double x[ndata]  = {1  ,2  ,3  ,4   ,5   } ; 
    const double y[ndata]  = {1.1,3.9,9.1,15.9,24.7} ; 
    const double ex[ndata] = {0.0,0.0,0.0,0.0 ,0.0 } ; 
    const double ey[ndata] = {0.1,0.2,0.3,0.1 ,0.2 } ; 

    const TVectorD v_x(ndata,x) ; 
    const TVectorD v_y(ndata,y) ; 
    const TVectorD v_ex(ndata,ex) ; 
    const TVectorD v_ey(ndata,ey) ; 

//    TGraphErrors *mygrapherrors = new TGraphErrors(v_x,v_y,v_ex,v_ey) ; 
    TGraphErrors *mygrapherrors = new TGraphErrors(ndata,x,y,ex,ey) ; 
    TF1 *myfunction = new TF1("myfunction","[0] * x*x",v_x.Min(),v_x.Max()) ; 
//    mygrapherrors->Fit(myfunction,"R") ; 
    TCanvas *mycanvas = new TCanvas("mycanvas","my canvas title") ; 
    mygrapherrors->Draw("AP") ; 
    mycanvas->Print("my.pdf") ;

    delete myfunction ; 
    delete mygrapherrors ; 
    delete mycanvas ; 
    return 0 ;
}

gives me no warning with valgrind but If I use the TGraphErrors constructor with TVectorD, I have 80 bytes definetely lost and if I fit myfunction, I have 1,306 bytes possibly lost…

Can you tell me if this is normal

Thanks in advance :slight_smile:

My system is :
ROOT v5-34-15
Linux 3.10.17 #1 SMP Sat Nov 2 11:20:07 EDT 2013 x86_64 GNU/Linux
x86_64 Debian Wheezy
gcc (Debian 4.7.2-5) 4.7.2

Hi,

Yes there is a problem with the constructor from TVectorD. The internal function list of TGraph is constructed two times, causing a memory leak. I will fix now in 5.34 and the master branches.

Instead, when fitting the possible lost are expected due to the static allocation in Minuit.If you add at the end of your macro, delete gMinuit, it will be better. Or you can use Minuit2.

Thanks for your report

Lorenzo