ROOT + valgrind

Dear Rooters,

For my work, I was using valgrind to debug and check some of the leaks. I’m still kinda new to this…
I would appreciate any help. It is such a comprehensive code. So i’d like to understand how fix other similar issues.

Valgrind gave me the following errors:

==2619== Conditional jump or move depends on uninitialised value(s)
==2619==    at 0x551A0FC: TObject::TObject() (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libCore.so)
==2619==    by 0x4F9B96C: TFile::Close(char const*) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libRIO.so)
==2619==    by 0x450BCA: ToyShowerNS::ToyShower::SetMPD(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&) (ToyShower.cc:1683)
==2619==    by 0x436472: treatSubBlock(float*, int) (resampler.cc:267)
==2619==    by 0x435A02: main (resampler.cc:170)

(>> some other stuff)

==2619== 1,081 (576 direct, 505 indirect) bytes in 1 blocks are definitely lost in loss record 33,723 of 34,288
==2619==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2619==    by 0x559AA28: TStorage::ObjectAlloc(unsigned long) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libCore.so)
==2619==    by 0x4592C5: TObject::operator new(unsigned long) (TObject.h:156)
==2619==    by 0x450590: ToyShowerNS::ToyShower::SetMPD(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&) (ToyShower.cc:1642)
==2619==    by 0x436472: treatSubBlock(float*, int) (resampler.cc:267)
==2619==    by 0x435A02: main (resampler.cc:170)

(>> some other stuff)

==2619== 12,193 (184 direct, 12,009 indirect) bytes in 1 blocks are definitely lost in loss record 34,161 of 34,288
==2619==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2619==    by 0x559AA28: TStorage::ObjectAlloc(unsigned long) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libCore.so)
==2619==    by 0x4592C5: TObject::operator new(unsigned long) (TObject.h:156)
==2619==    by 0x4506E5: ToyShowerNS::ToyShower::SetMPD(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&) (ToyShower.cc:1646)
==2619==    by 0x436472: treatSubBlock(float*, int) (resampler.cc:267)
==2619==    by 0x435A02: main (resampler.cc:170)

The function ToyShower::SetMPD(…) is:


void ToyShower::SetMPD(std::vector< std::vector<double> >& vMPD) {

  TH1D *hMPD = new TH1D("hMPD","hMPD",600.,0.,3000.);   //bin size of 5 [g/cm2]

  double hX = UNDEF, h = UNDEF, theta = UNDEF, weight = UNDEF;
  double minX = UNDEF, maxX = UNDEF;

  double Xmumax = UNDEF, Nmumax = UNDEF, hmumax = UNDEF, chi2 = UNDEF;

  for(int i =0 ; i< vMPD.size(); i++){

    theta  = acos(vMPD[i][1]);
    h      = vMPD[i][2];
    weight = vMPD[i][3];

    hX = fatm.HeightToSlantDepth(h,theta);   //inclined X
    if( ToyUtilsNS::X_AugerGroundLevel(hX,theta) ) continue;
    hMPD->Fill(hX, weight);
  }

  minX = hMPD->GetBinCenter(hMPD->GetMaximumBin()) - 200./cos(theta);
  maxX = hMPD->GetBinCenter(hMPD->GetMaximumBin()) + 300./cos(theta);
  maxX = (maxX > 875/cos(theta) ? 875/cos(theta) : maxX);

  TF1 *ghFunc  = new TF1("ghFunc",ToyShowerNS::GHFunc, minX , maxX , 4);
  ghFunc->SetParameters(3*hMPD->GetMaximum(), hMPD->GetBinCenter(hMPD->GetMaximumBin()) , -1. , 70.);

  //--- ROOT5 has some issues calculating chi2 with TH1. It is somehow resolved with ROOT6 (i use tgraph as a workaround for ROOT5)
  TGraphErrors *grMPD = new TGraphErrors();
  grMPD->SetName(Form("%dMPD",GetShowerId()));
  int nbinsx = hMPD->GetXaxis()->GetNbins();
  double rebin_x = 0., rebin_y = 0.,rebin_yerr = 0., bincnt = 0.;

  for(int ih1 = 0; ih1 < nbinsx; ih1++){
    int ihgr = grMPD->GetN();
    bincnt++;
    rebin_x += hMPD->GetBinCenter(ih1);
    rebin_y += hMPD->GetBinContent(ih1);
    rebin_yerr += (hMPD->GetBinContent(ih1)) * (hMPD->GetBinContent(ih1));

    if ((int) bincnt == 3 || ih1 == (nbinsx-1)) {
      rebin_x = rebin_x / bincnt;
      rebin_yerr = sqrt( (bincnt/ (bincnt-1.)) * (rebin_yerr/ bincnt - (rebin_y / bincnt)*(rebin_y / bincnt)) ); //variance of those those bins

      grMPD->SetPoint(ihgr,rebin_x,rebin_y);
      grMPD->SetPointError(ihgr,0.,rebin_yerr);

      rebin_x = 0., rebin_y = 0.,rebin_yerr = 0., bincnt = 0.;
    }
  }

  grMPD->Fit("ghFunc", "QR");

  //---
  //ghFunc->SetParameters(hMPD->GetMaximum(), hMPD->GetBinCenter(hMPD->GetMaximumBin()) , -1. , 70.);
  //hMPD->Fit("ghFunc", "R");

  chi2     = (ghFunc->GetChisquare()) / ( ((maxX - minX) / 15.) - 4.);
  Nmumax   = ghFunc->GetParameter(0);
  Xmumax   = ghFunc->GetParameter(1);
  hmumax   = fatm.SlantDepthToHeight(Xmumax, theta);
  printf("Xmumax from GH-fit: %lf[g/cm2] with chi2: %lf \n" , Xmumax , chi2);

  TFile *f = new TFile(Form("%s/%d.rLONG",ProductionDataPath,GetShowerId()),"update");
  grMPD->Write();
  f->Close();

  delete hMPD, ghFunc, grMPD, f;

  cinfo.Xmumax     = Xmumax;
  cinfo.Chi2Xmumax = chi2;
  cinfo.hmumax     = hmumax;
  cinfo.Nmumax     = Nmumax;
}

When running valgrind, do you use:

--suppressions=`root-config --etcdir`/valgrind-root.supp

You have duplicated code. Why are you recreating two objects in your code without previously deleting them?

TF1 *ghFunc  = new TF1("ghFunc",ToyShowerNS::GHFunc, minX , maxX , 4);
  ghFunc->SetParameters(3*hMPD->GetMaximum(), hMPD->GetBinCenter(hMPD->GetMaximumBin()) , -1. , 70.);
  TGraphErrors *grMPD = new TGraphErrors();

 (...)

  TF1 *ghFunc  = new TF1("ghFunc",ToyShowerNS::GHFunc, minX , maxX , 4);
  ghFunc->SetParameters(3*hMPD->GetMaximum(), hMPD->GetBinCenter(hMPD->GetMaximumBin()) , -1. , 70.);
  TGraphErrors *grMPD = new TGraphErrors();

should be rather:


TF1 *ghFunc  = new TF1("ghFunc",ToyShowerNS::GHFunc, minX , maxX , 4);
  ghFunc->SetParameters(3*hMPD->GetMaximum(), hMPD->GetBinCenter(hMPD->GetMaximumBin()) , -1. , 70.);
  TGraphErrors *grMPD = new TGraphErrors();

 (...)

delete ghFunc, grMPD;
  ghFunc  = new TF1("ghFunc",ToyShowerNS::GHFunc, minX , maxX , 4);
  ghFunc->SetParameters(3*hMPD->GetMaximum(), hMPD->GetBinCenter(hMPD->GetMaximumBin()) , -1. , 70.);
  grMPD = new TGraphErrors();

Hi,

Thx for your reply.
Code wasn’t duplicated. I just copied it incorrectly and accidentally duplicated a portion here in the forum. I removed the duplicated portion in my edit just now.

Hi,

No i didn’t.
I ran my code again with the options you showed.

Now, the output is (it is actually repeated with an increasing amount of bytes):

==31333== 1 bytes in 1 blocks are still reachable in loss record 3 of 34,287
==31333==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31333==    by 0x559EA09: TBits::TBits(unsigned int) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libCore.so)
==31333==    by 0x5ED1A3F: TFormula::TFormula() (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x5E0C928: TF1::TF1() (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x5F36FB4: ROOTDict::new_TF1(void*) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x55E5E77: TClass::New(TClass::ENewType) const (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libCore.so)
==31333==    by 0x5E44289: void HFit::StoreAndDrawFitFunction<TGraph>(TGraph*, TF1*, ROOT::Fit::DataRange const&, bool, bool, char const*) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x5E47C1A: TFitResultPtr HFit::Fit<TGraph>(TGraph*, TF1*, Foption_t&, ROOT::Math::MinimizerOptions const&, char const*, ROOT::Fit::DataRange&) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x5E42D7E: ROOT::Fit::FitObject(TGraph*, TF1*, Foption_t&, ROOT::Math::MinimizerOptions const&, char const*, ROOT::Fit::DataRange&) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x5E682DE: TGraph::Fit(TF1*, char const*, char const*, double, double) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x5E668F4: TGraph::Fit(char const*, char const*, char const*, double, double) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x450A52: ToyShowerNS::ToyShower::SetMPD(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&) (ToyShower.cc:1669)
==31333==
==31333== 1 bytes in 1 blocks are indirectly lost in loss record 4 of 34,287
==31333==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==31333==    by 0x559EA09: TBits::TBits(unsigned int) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libCore.so)
==31333==    by 0x5ED1A3F: TFormula::TFormula() (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x5E0DAE8: TF1::TF1(char const*, double (*)(double*, double*), double, double, int) (in /cr/augerdata/OfflineSource/Externals/ev17/s4/External/root/5.34.00-14d831f5b4/lib/root/libHist.so)
==31333==    by 0x4505CD: ToyShowerNS::ToyShower::SetMPD(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&) (ToyShower.cc:1642)
==31333==    by 0x436472: treatSubBlock(float*, int) (resampler.cc:267)
==31333==    by 0x435A02: main (resampler.cc:170)

Hi,

you never indicated which version of ROOT and which arch
you are using.

From the valgrind logs I conclude: 5.34.00, more than 5 years old,
even for a long term project like ROOT quite some time.

Cheers
Otto

Hi,

My apologies. I neglected to inform you on this matter. I am using ROOT 5.34.00.
Do you expect this to be related with the version? I could try with ROOT5.34.36 (but not ROOT6).

Cheers,
Johan

To me, the “14d831f5b4” in the subdirectory name suggests that this is the “v5-34-00-patches branch” from Jan 9th 2017.

Try the current head of v5-34-00-patches branch.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.