LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2013, 04:02 AM   #1
tristandnv
LQ Newbie
 
Registered: Jan 2013
Location: Geneva
Posts: 6

Rep: Reputation: Disabled
free(): invalid next size(fast) // malloc(): memory corruption - ROOT CERN


Hello dear all,

I'm a Physicist and I come here for help from Programmers as you with much more knowledge and experience...

I wrote a program that finds all the hits and identifies those which overlap from data taken with an oscilloscope. In event::reduce() (event class corresponds to one file-profile and has members as vector<hit>, noise, threshold, etc, where hit is another class corresponding to a single pulse - I can attach the scripts under request) I identify the superpositions, each hit has a label member which sorts them out (same group number means pulses overlapping). Then, in analysis.cxx, the code below, I reduce all the hits. I fit the pulses overlapping and create two event branches, one with the original info (hits without fit parameters) and another one with hits with the fit parameters, a branch TMultiGraph with the graphs of the superpositions and another branch TClonesArray with TFitResult instances of the fits. Apparently everything works well when I process a group of files with only one file with a superposition (file 12 is first file with a superposition). So, when I run ./analysis.exe /eth/ethraid5/home/dnarrias/data_Daniel-Thomas/15Nov/Waveforms MI-1_687_ampl 20 RT_687_test.root, the program works, but when I run on 30 instead of 20 files, when processing file 29 with superpositions, the programs crashes. May the problem be an allocation error/accessing non allocated memory one?. Unfortunately, I don't see where the problem is in the code... The error message I get is the following one:

Code:
*** glibc detected *** ./analysis.exe: free(): invalid next size (fast): 0x0a38dc90 ***
======= Backtrace: =========
/lib/libc.so.6[0x78c3ee5]
/lib/libc.so.6(cfree+0x59)[0x78c4329]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0x46455c1]
/usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0x464561d]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libCore.so(_ZN7TStringD1Ev+0x35)[0x8eb69a5]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libCore.so(_ZN14TPluginManager26LoadHandlersFromPluginDirsEPKc+0xc7)[0x8e91e77]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libCore.so(_ZN14TPluginManager11FindHandlerEPKcS1_+0x28)[0x8e93638]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so(_ZN4ROOT4Math7Factory15CreateMinimizerERKSsS3_+0xd7)[0x2dc6cb7]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so(_ZN4ROOT3Fit9FitConfig15CreateMinimizerEv+0x61)[0x2dc83c1]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so(_ZN4ROOT3Fit6Fitter15DoInitMinimizerEv+0xcb)[0x2dd3d2b]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so(_ZN4ROOT3Fit6Fitter14DoMinimizationERKNS_4Math21IBaseFunctionMultiDimEPS4_+0x48)[0x2dd4a48]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so(_ZN4ROOT3Fit6Fitter16DoLeastSquareFitERKNS0_7BinDataE+0x378)[0x2dd6348]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so(_ZN4HFit3FitI6TGraphEE13TFitResultPtrPT_P3TF1R9Foption_tRKN4ROOT4Math16MinimizerOptionsEPKcRNS9_3Fit9DataRangeE+0xb44)[0x4d13e4]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so(_ZN4ROOT3Fit9FitObjectEP6TGraphP3TF1R9Foption_tRKNS_4Math16MinimizerOptionsEPKcRNS0_9DataRangeE+0x57)[0x4ca047]
/eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so(_ZN6TGraph3FitEP3TF1PKcS3_dd+0x160)[0x53ecc0]
./analysis.exe(_Z12OverlapSplitPdidRPiddiRP11TMultiGraphR12TClonesArrayid+0x814)[0x804c92e]
./analysis.exe(main+0x92b)[0x804d527]
/lib/libc.so.6(__libc_start_main+0xdc)[0x7871e9c]
./analysis.exe(_ZN10TCanvasImp8StreamerER7TBuffer+0x39)[0x804bfe1]
======= Memory map: ========
00110000-00287000 r-xp 00000000 00:1a 17343246 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libGraf.so
00287000-0028f000 rwxp 00177000 00:1a 17343246 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libGraf.so
0028f000-00297000 rwxp 0028f000 00:00 0
00297000-002df000 r-xp 00000000 00:1a 17343333 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libPostscript.so
002df000-002e1000 rwxp 00048000 00:1a 17343333 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libPostscript.so
002e1000-00325000 r-xp 00000000 00:1a 17343202 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libThread.so
00325000-00328000 rwxp 00044000 00:1a 17343202 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libThread.so
00328000-0032b000 r-xp 00000000 fd:00 13632988 /lib/libdl-2.5.so
0032b000-0032c000 r-xp 00002000 fd:00 13632988 /lib/libdl-2.5.so
0032c000-0032d000 rwxp 00003000 fd:00 13632988 /lib/libdl-2.5.so
0032e000-00349000 r-xp 00000000 fd:00 13631499 /lib/ld-2.5.so
00349000-0034a000 r-xp 0001a000 fd:00 13631499 /lib/ld-2.5.so
0034a000-0034b000 rwxp 0001b000 fd:00 13631499 /lib/ld-2.5.so
0034b000-003db000 r-xp 00000000 00:1a 17343332 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libPhysics.so
003db000-003de000 rwxp 00090000 00:1a 17343332 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libPhysics.so
003de000-003fd000 r-xp 00000000 00:13 8155630 /eth/ethraid5/home/dnarrias/data_Daniel-Thomas/photondet/trace/PMTSignalsAnalysiscam/pmtTools/libPmtTools.so
003fd000-003fe000 rwxp 0001e000 00:13 8155630 /eth/ethraid5/home/dnarrias/data_Daniel-Thomas/photondet/trace/PMTSignalsAnalysiscam/pmtTools/libPmtTools.so
003fe000-00425000 r-xp 00000000 fd:00 13632989 /lib/libm-2.5.so
00425000-00426000 r-xp 00026000 fd:00 13632989 /lib/libm-2.5.so
00426000-00427000 rwxp 00027000 fd:00 13632989 /lib/libm-2.5.so
00430000-00431000 r-xp 00430000 00:00 0 [vdso]
00431000-00875000 r-xp 00000000 00:1a 17343244 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so
00875000-00889000 rwxp 00443000 00:1a 17343244 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so
00889000-0088c000 rwxp 00889000 00:00 0
0088c000-00a81000 r-xp 00000000 00:1a 17343245 /eth/sw/ROOTAborted
The analysis.cxx script is below.

Code:
//creating fit Functor
class  exp_functor {
public:

        exp_functor(int nn){ nHits = nn;}

        double operator() (const double *x, const double *p) {
                double fit = 0.;
                for(int i = 0; i < nHits; i++)  fit += hit(x[0], p[3*i], p[3*i+1], p[3*i+2]);
                return fit;
        }

        //this is the fit function for one hit
        double hit(double x, double p0, double p1, double p2){

                double h = p0*(x-p1)*exp(-(x-p1)/p2);
                if(x < p1) return 0.;
                        else return h;
        }

private:
        int nHits;
};


double* OverlapSplit(double* inputPars, int nHitsOverlap, double lastWidth, int*& wave, double vGain, double vOffset, int nevent, TMultiGraph*& MultiGOverlap, TClonesArray& fitResultArray, int nOverlap, double pedestal){
//nOverlap is the number of an overlap in an event.     
// inputPars[n]:StartTime, inputPars[n+1]:riseTime, inputPars[n+2]:maximum. n: number of hit in the superposition. Only vertical parameters are in V, horizontal parameters (times) are in ns (sampling time 4ns).

        double* outputPars = new double[40];
// outputPars[n]: riseTime, outputPars[n+1]:scalar (= maximum* e / riseTime), output[n+2]:offset (startTime).
        double *hitOverlap = new double[200];//let's assume a hit has 50 samples, 50*4=200ns width. If we consider superpositions with up to 4 hits, 50*4=200 samples:number of elements in this array. 

        const double e = exp(1.0);

        double StartOverlap = *inputPars;

        *hitOverlap = 0.;

        //At this point start's and width's are double but integer numbers, ex. 4., 12., etc.

        for(int ii = (int)(StartOverlap/4); ii <= (int)(*(inputPars + 3*(nHitsOverlap-1))) + (int)(lastWidth/4); ii++) *(hitOverlap + ii + 1 - (int)(StartOverlap/4)) = (vGain*(double)wave[ii] - vOffset)-pedestal;
        for(int ii = (int)(*(inputPars + 3*(nHitsOverlap-1))) + (int)(lastWidth/4) + 1; ii < (int)(StartOverlap/4) + 199; ii++) *(hitOverlap + ii + 1 - (int)(StartOverlap/4)) = 0.;

        double *times = new double[200];
        for(int ii = 0; ii < 200; ii++) *(times + ii) = StartOverlap + 4*(ii - 1);//time in ns, [StartOverlap-4, StartOverlap + 199*4).

        TGraph *GOverlap = new TGraph(200, times, hitOverlap);
        char *overlapName = new char[200];
        sprintf(overlapName, "startOverlap%dns_nOverlap%d_event%d", StartOverlap, nOverlap, nevent);
        GOverlap->SetName(overlapName);
        char *overlapTitle = new char[200];
        char *startTimesTitle = new char[180];
        sprintf(startTimesTitle, "Start Hit1: %dns, Start Hit%d: %dns ", *inputPars, nHitsOverlap, *(inputPars + 3*(nHitsOverlap-1)));
        sprintf(overlapTitle, "%s; nOverlap: %d; event: %d", startTimesTitle, nOverlap, nevent);
        GOverlap->SetTitle(overlapTitle);
        GOverlap->SetMarkerStyle(20);
        GOverlap->SetMarkerSize(1);
        GOverlap->SetMarkerColor(1);

        char *fitFunctName = new char[80];
        sprintf(fitFunctName, "fitfunction_startOverlap%dns_event%d", StartOverlap, nevent);

        exp_functor *fit = new exp_functor(nHitsOverlap);

        TF1 *fitFunction = new TF1("fitFunction", fit, StartOverlap -4, StartOverlap -4 + 4*100, 3*nHitsOverlap, "exp_functor");//+4*200 but we assume last half is empty data

        for(int i = 0; i < nHitsOverlap; i++){
                 fitFunction->SetParameter( 3*i, e*(*(inputPars + 3*i + 2))/(*(inputPars + 3*i + 1)));
                 fitFunction->SetParameter( 3*i + 1, *(inputPars + 3*i));
                 fitFunction->SetParameter( 3*i + 2, *(inputPars + 3*i + 1));
        }

        char *ParName = new char[15];
        char *scalar = new char[10];
        char *offset = new char[10];
        char *riseTime = new char[10];

        sprintf(scalar, "scalar");
        sprintf(offset, "offset");
        sprintf(riseTime, "riseTime");

        for(int i = 0; i < nHitsOverlap; i++){
                sprintf(ParName, "%s%d", scalar, i);
                fitFunction->SetParName(3*i, ParName);
                sprintf(ParName, "%s%d", offset, i);
                fitFunction->SetParName(3*i + 1, ParName);
                sprintf(ParName, "%s%d", riseTime, i);
                fitFunction->SetParName(3*i + 2, ParName);
        }

        fitFunction->SetLineColor(2);

        TFitResultPtr fitResultPtr;
        fitResultPtr = GOverlap->Fit(fitFunction, "RS");

        new(fitResultArray[nOverlap]) TFitResult((*fitResultPtr.Get()));

        MultiGOverlap->Add(GOverlap);

        //we could get all the parameters in one line: const double* TFitResult::GetParams(), but we want to keep our dynamically allocated pointer outputPars.
        for(int i = 0; i < nHitsOverlap; i++){
                *(outputPars + 3*i) = fitFunction->GetParameter(3*i + 2);
                *(outputPars + 3*i + 1) = fitFunction->GetParameter(3*i);
                *(outputPars + 3*i + 2) = fitFunction->GetParameter(3*i + 1);

        }

        delete[] overlapTitle;
        delete[] startTimesTitle;
        delete[] fitFunctName;
        delete[] ParName;
        delete[] scalar;
        delete[] offset;
        delete[] riseTime;
        fitFunction->TF1::~TF1();

        delete[] overlapName;
        delete[] times;
        delete[] hitOverlap;
        return outputPars;
}



//outputs a root file with reduced events
int main(int argc, char *argv[]){

        if(argc != 5){
                fprintf(stderr, "%s <inputFolderName> <inputFileBaseName> <numberOfEvents> <outputFileName>\n\n", argv[0]);
                return 1;
        }
        int ii, cnt = 0, nn = atoi(argv[3]);
        FILE * file;
        int *wave;
        double vGain, vOffset;
        TFile * out = new TFile(argv[4], "RECREATE");
        TTree * tree = new TTree("tree", argv[4]);

        waveform * wa = new waveform();
        event * ev = new event();
        event * rEv = new event();
        //OBS: TClonesArray and TObjArray can only collect classes which inherit from TObject. TFitResultPtr doesn't inherit from TObject. On the other hand, TFitResult does inherit from TObject.
        TClonesArray *PfitResultArray = new TClonesArray("TFitResult", 6);
        TClonesArray &fitResultArray = *PfitResultArray;
        TMultiGraph *MultiGOverlap = new TMultiGraph("MultiGOverlaps", "MultiGOverlaps");//default constructor
        tree->Branch("Event", "event", &ev, 32000, 0);
        tree->Branch("reducedEvent", "event", &rEv, 32000, 0);
        tree->Branch("FitResultArray", "TClonesArray", &PfitResultArray, 32000, 0);
        tree->Branch("MultiGraphOverlap", "TMultiGraph", &MultiGOverlap, 32000, 0) ;

        double* inputPars = new double[40];
        double* outputPars = new double[40];
        int nHitsOverlap = 1;
        int nOverlap = 0;
        double lastWidth;
        vector<hit> hits;
        hit Hit, prevHit;
        double pedestal;

       const double e = exp(1.0);
        const double a = exp(-5.0);
        double scalar, riseTime, offset, integral, maximum;

        char fileName[128];
        for(ii = 0; ii < nn; ii++){

                sprintf(fileName, "%s/C1%s%05d.trc", argv[1], argv[2], ii);

                if(ii < 60) continue; //.trc file is corrupt           

                if((file = fopen(fileName, "r")) == NULL){
                        fclose(file);
                        continue;
                }
                wa->importLeCroyBinary(file);
                fclose(file);
                if((wa->getWave()) == NULL){
                        continue;
                }
                ev->reduce(wa, 32, 2., 6);// 32*4ns = 128ns < pedestalWidth; 2.*noise = threshold; 6*4ns=24ns < minimumHitWidth accepted.

                *rEv = *ev;

                if(ev->getnHits() > 1){

                        wave = wa->getWave();
                        vGain = wa->getyDelta();
                        vOffset = wa->getyOffset();
                        hits = ev->getHits();
                        pedestal = ev->getPedestal();
                        for(int j = 1; j < ev->getnHits(); j++){
                                prevHit = hits.at(j-1);
                                Hit = hits.at(j);
                                if(prevHit.getGroupOverlap() == Hit.getGroupOverlap()){

                                        *(inputPars + 3*(nHitsOverlap-1)) = prevHit.getStart();//in ns units
                                        *(inputPars + 3*(nHitsOverlap-1) + 1) = prevHit.getRiseTime();
                                        *(inputPars + 3*(nHitsOverlap-1) + 2) = prevHit.getMaximum();
                                        *(inputPars + 3*nHitsOverlap) = Hit.getStart();
                                        *(inputPars + 3*nHitsOverlap + 1) = Hit.getRiseTime();
                                        *(inputPars + 3*nHitsOverlap + 2) = Hit.getMaximum();

                                        lastWidth = Hit.getWidth();
                                        nHitsOverlap++;

                                        if(j == (ev->getnHits()-1)){
                                                printf("nHits: %d, event %d\n", ev->getnHits(), ii);

                                                outputPars = OverlapSplit(inputPars, nHitsOverlap, lastWidth, wave, vGain, vOffset, ii, MultiGOverlap, fitResultArray, nOverlap, pedestal);

                                                printf("Hola");

                                                for(int k = 0; k < nHitsOverlap; k++){

                                                        riseTime = *(outputPars + 3*k);
                                                        scalar = *(outputPars + 3*k + 1);
                                                        offset = *(outputPars + 3*k + 2);
                                                        maximum = scalar*riseTime/e + pedestal;
                                                        integral = scalar*riseTime*riseTime*(1-6*a)/5 + 5*riseTime*pedestal/5;//to obtain the charge, the integral must be divided by 50Ohm, we divide by 5 and the charge is in [e-10 C] units ---- With the model we use for hit fitting, time(9%ofMaximum)=5.022*riseTime, which we consider as hitWidth. By adding the last term to the integral, we're allowed to plot charge with pedestal charge and then correct the charge.

                                                        printf("Pedestal: %f V\n", pedestal);
                                                        printf("Start%d\tnOverlap%d\tevent%d\n", (int)(*inputPars), nOverlap, ii);
                                                        printf("-------------------- Hit%d\tStart: (a)%f (b)%f\tPeakTime: (a)%f (b)%f\tRisetime: (a)%f (b)%f\tWidth: (a)%f (b)%f\n-------------------------\tMaximum: (a)%f (b)%f\tIntegral: (a)%f (b)%f\n", k, (hits.at(j-nHitsOverlap+k+1)).getStart(), offset, (hits.at(j-nHitsOverlap+k+1)).getPeakTime(), offset + riseTime, (hits.at(j-nHitsOverlap+k+1)).getRiseTime(), riseTime, (hits.at(j-nHitsOverlap+k+1)).getWidth(), 5*riseTime, (hits.at(j-nHitsOverlap+k+1)).getMaximum(), maximum, (hits.at(j-nHitsOverlap+k+1)).getIntegral(), integral);

                                                        hit tmp;
                                                        tmp.init(offset, riseTime, 5*riseTime, offset + riseTime, maximum, integral, prevHit.getGroupOverlap());
                                                        rEv->replaceHit(j-nHitsOverlap+k+1, tmp);
                                                }
                                        }
                                } else if(nHitsOverlap > 1){
                                                printf("nHits: %d, event %d\n", ev->getnHits(), ii);

                                                outputPars = OverlapSplit(inputPars, nHitsOverlap, lastWidth, wave, vGain, vOffset, ii, MultiGOverlap, fitResultArray, nOverlap, pedestal);

                                                for(int k = 0; k < nHitsOverlap; k++){

                                                        riseTime = *(outputPars + 3*k);
                                                        scalar = *(outputPars + 3*k + 1);
                                                        offset = *(outputPars + 3*k + 2);
                                                        maximum = scalar*riseTime/e + pedestal;
                                                        integral = scalar*riseTime*riseTime*(1-6*a)/5 + 5*riseTime*pedestal/5;

                                                        printf("Pedestal: %f V\n", pedestal);
                                                        printf("Start%d\tnOverlap%d\tevent%d\n",(int)(*inputPars), nOverlap, ii);
                                                        printf("-------------------- Hit%d\tStart: (a)%f (b)%f\tPeakTime: (a)%f (b)%f\tRisetime: (a)%f (b)%f\tWidth: (a)%f (b)%f\n-------------------------\tMaximum: (a)%f (b)%f\tIntegral: (a)%f (b)%f\n", k, (hits.at(j-nHitsOverlap+k)).getStart(), offset, (hits.at(j-nHitsOverlap+k)).getPeakTime(), offset + riseTime, (hits.at(j-nHitsOverlap+k)).getRiseTime(), riseTime, (hits.at(j-nHitsOverlap+k)).getWidth(), 5*riseTime, (hits.at(j-nHitsOverlap+k)).getMaximum(), maximum, (hits.at(j-nHitsOverlap+k)).getIntegral(), integral);

                                                        hit tmp;
                                                        tmp.init(offset, riseTime, 5*riseTime, offset + riseTime, maximum, integral, prevHit.getGroupOverlap());//initializes a hit
                                                        rEv->replaceHit(j-nHitsOverlap+k, tmp);

                                                }

                                                nOverlap++;
                                                nHitsOverlap = 1;
                                        }
                        }

                }

                nOverlap = 0;
                nHitsOverlap = 1;

                if(ii%1000 == 0) fprintf(stderr, "%6d\t%6d\r", cnt, nn);
                cnt++;


                tree->Fill();
                //Reinitialize fitResult and MultiGOverlap.
                fitResultArray.Clear();
                MultiGOverlap->TMultiGraph::~TMultiGraph();
                MultiGOverlap = new TMultiGraph("MultiGOverlaps", "MultiGOverlaps");
                hits.clear();

        }

        fprintf(stderr, "%6d\t%6d\n", cnt, nn);
        delete wa;
        delete ev;
        delete rEv;

        tree->Print();
        tree->Write();
        out->Close();

        return 0;
}
Below find a bt from gdb.

Code:
#0 0x00f2c402 in __kernel_vsyscall ()
#1 0x053eedf0 in raise () from /lib/libc.so.6
#2 0x053f0701 in abort () from /lib/libc.so.6
#3 0x05425bcb in __libc_message () from /lib/libc.so.6
#4 0x0542dee5 in _int_free () from /lib/libc.so.6
#5 0x0542e329 in free () from /lib/libc.so.6
#6 0x026ba5c1 in operator delete(void*) () from /usr/lib/libstdc++.so.6
#7 0x026ba61d in operator delete[](void*) () from /usr/lib/libstdc++.so.6
#8 0x058229a5 in TString::~TString() () from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libCore.so
#9 0x057fde77 in TPluginManager::LoadHandlersFromPluginDirs(char const*) () from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libCore.so
#10 0x057ff638 in TPluginManager::FindHandler(char const*, char const*) () from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libCore.so
#11 0x035a6cb7 in ROOT::Math::Factory::CreateMinimizer(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so
#12 0x035a83c1 in ROOT::Fit::FitConfig::CreateMinimizer() () from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so
#13 0x035b3d2b in ROOT::Fit::Fitter::DoInitMinimizer() () from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so
#14 0x035b4a48 in ROOT::Fit::Fitter::DoMinimization(ROOT::Math::IBaseFunctionMultiDim const&, ROOT::Math::IBaseFunctionMultiDim const*) ()
from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so
#15 0x035b6348 in ROOT::Fit::Fitter::DoLeastSquareFit(ROOT::Fit::BinData const&) () from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMathCore.so
#16 0x003eb3e4 in TFitResultPtr HFit::Fit<TGraph>(TGraph*, TF1*, Foption_t&, ROOT::Math::MinimizerOptions const&, char const*, ROOT::Fit::DataRange&) ()
from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so
#17 0x003e4047 in ROOT::Fit::FitObject(TGraph*, TF1*, Foption_t&, ROOT::Math::MinimizerOptions const&, char const*, ROOT::Fit::DataRange&) ()
from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so
#18 0x00458cc0 in TGraph::Fit(TF1*, char const*, char const*, double, double) () from /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so
#19 0x0804c8b9 in OverlapSplit(double*, int, double, int*&, double, double, int, TMultiGraph*&, TClonesArray&, int, int, double) ()
#20 0x0804d8bd in main ()
but it doesn't help me much...

When I exclude the first 60 files and run the executable until the file number 95, the execution finishes well and it processes fine all the files with superpositions, but when run on files until the number 100, it crashes. The error message I get is the following:

Code:
*** glibc detected *** ./analysis.exe: malloc(): memory corruption: 0x08fda278 ***
======= Backtrace: =========
/lib/libc.so.6[0x2042c1d]
/lib/libc.so.6(__libc_malloc+0x67)[0x20447d7]
/usr/lib/libstdc++.so.6(_Znwj+0x27)[0x8730b87]
/usr/lib/libstdc++.so.6(_Znaj+0x1d)[0x8730cbd]
./analysis.exe(_Z12OverlapSplitPdidRPiddiRP11TMultiGraphR12TClonesArrayid+0x25a)[0x804c374]
./analysis.exe(main+0x938)[0x804d534]
/lib/libc.so.6(__libc_start_main+0xdc)[0x1fefe9c]
./analysis.exe(_ZN10TCanvasImp8StreamerER7TBuffer+0x39)[0x804bfe1]
======= Memory map: ========
00110000-0013d000 r-xp 00000000 00:1a 17343334 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libRint.so
0013d000-0013f000 rwxp 0002c000 00:1a 17343334 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libRint.so
0013f000-00187000 r-xp 00000000 00:1a 17343333 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libPostscript.so
00187000-00189000 rwxp 00048000 00:1a 17343333 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libPostscript.so
00189000-0018c000 r-xp 00000000 fd:00 13632988 /lib/libdl-2.5.so
0018c000-0018d000 r-xp 00002000 fd:00 13632988 /lib/libdl-2.5.so
0018d000-0018e000 rwxp 00003000 fd:00 13632988 /lib/libdl-2.5.so
0018e000-00199000 r-xp 00000000 fd:00 13632373 /lib/libgcc_s-4.1.2-20080825.so.1
00199000-0019a000 rwxp 0000a000 fd:00 13632373 /lib/libgcc_s-4.1.2-20080825.so.1
001a0000-001bf000 r-xp 00000000 00:13 8155680 /eth/ethraid5/home/dnarrias/data_Daniel-Thomas/photondet/trace/PMTSignalsAnalysiscam/pmtTools/libPmtTools.so
001bf000-001c0000 rwxp 0001e000 00:13 8155680 /eth/ethraid5/home/dnarrias/data_Daniel-Thomas/photondet/trace/PMTSignalsAnalysiscam/pmtTools/libPmtTools.so
001c2000-001c3000 r-xp 001c2000 00:00 0 [vdso]
001c3000-002c2000 r-xp 00000000 00:1a 17343282 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libGraf3d.so
002c2000-002c8000 rwxp 000fe000 00:1a 17343282 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libGraf3d.so
002c8000-002cb000 rwxp 002c8000 00:00 0
002cb000-0030f000 r-xp 00000000 00:1a 17343202 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libThread.so
0030f000-00312000 rwxp 00044000 00:1a 17343202 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libThread.so
00312000-00327000 r-xp 00000000 fd:00 13631685 /lib/libpthread-2.5.so
00327000-00328000 --xp 00015000 fd:00 13631685 /lib/libpthread-2.5.so
00328000-00329000 r-xp 00015000 fd:00 13631685 /lib/libpthread-2.5.so
00329000-0032a000 rwxp 00016000 fd:00 13631685 /lib/libpthread-2.5.so
0032a000-0032c000 rwxp 0032a000 00:00 0
0032e000-00349000 r-xp 00000000 fd:00 13631499 /lib/ld-2.5.so
00349000-0034a000 r-xp 0001a000 fd:00 13631499 /lib/ld-2.5.so
0034a000-0034b000 rwxp 0001b000 fd:00 13631499 /lib/ld-2.5.so
0034b000-0078f000 r-xp 00000000 00:1a 17343244 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so
0078f000-007a3000 rwxp 00443000 00:1a 17343244 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libHist.so
007a3000-007a6000 rwxp 007a3000 00:00 0
007a6000-0091d000 r-xp 00000000 00:1a 17343246 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libGraf.so
0091d000-00925000 rwxp 00177000 00:1a 17343246 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libGraf.so
00925000-0092d000 rwxp 00925000 00:00 0
0092d000-00b22000 r-xp 00000000 00:1a 17343245 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libTree.so
00b22000-00b2c000 rwxp 001f5000 00:1a 17343245 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libTree.so
00b2c000-00b2e000 rwxp 00b2c000 00:00 0
00b2e000-00d70000 r-xp 00000000 00:1a 17343241 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMatrix.so
00d70000-00d77000 rwxp 00242000 00:1a 17343241 /eth/sw/ROOT/5.30.00/i386-linux/root/lib/libMatrix.so
00d77000-00d79000 rwxp 00d77000 00:00 0
00d79000-00da0000 r-xp 00000000 fd:00 13632989 /lib/libm-2.5.so
00da0000-00da1000 r-xp 00026000 fd:00 13632989 /lib/libm-2.5.so
00da1000-00da2000 rwxp 00027000 fd:00 13632989 /lib/libm-2.5.so
00da2000-00db4000 r-xp 00000000 fd:00 13632991 /lib/libz.so.1.2.3
00db4000-00db5000 rwxp 00011000 fd:00 13632991 /lib/libz.so.1.2.3
00db5000-00dd4000 r-xp 00000000 fd:00 13632926 /lib/libpcre.so.0.0.1
Aborted
and the bt is

Code:
#0 0x00360402 in __kernel_vsyscall ()
#1 0x06ec6df0 in raise () from /lib/libc.so.6
#2 0x06ec8701 in abort () from /lib/libc.so.6
#3 0x06efdbcb in __libc_message () from /lib/libc.so.6
#4 0x06f06c1d in _int_malloc () from /lib/libc.so.6
#5 0x06f087d7 in malloc () from /lib/libc.so.6
#6 0x00c97b87 in operator new(unsigned int) () from /usr/lib/libstdc++.so.6
#7 0x00c97cbd in operator new[](unsigned int) () from /usr/lib/libstdc++.so.6
#8 0x0804c374 in OverlapSplit(double*, int, double, int*&, double, double, int, TMultiGraph*&, TClonesArray&, int, double) ()
#9 0x0804d534 in main ()
I'm puzzled...

I know that only with this information it's difficult for you to figure out how to fix the problem. Please let me know if interested and I send you the whole macro so you can run the program. It's a hugeee program with all the class implementations ( event, hit) and other classes to get data from binary files and handle the data ( waveform, readLeCroyBinary classes, etc), let alone the necessary data binary files.

I hope that someone looking for challenging code/ROOT problems gets interested in this one.
Thanks in advance.
Daniel

Last edited by tristandnv; 02-12-2013 at 09:27 AM.
 
Old 02-12-2013, 05:05 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 12,297

Rep: Reputation: 3803Reputation: 3803Reputation: 3803Reputation: 3803Reputation: 3803Reputation: 3803Reputation: 3803Reputation: 3803Reputation: 3803Reputation: 3803Reputation: 3803
I would suggest you to try valgrind (http://valgrind.org/). Also try to compile in debug mode.
 
Old 02-12-2013, 09:07 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Please use CODE blocks, not QUOTE blocks for text copy/pasted from your computer. The QUOTE blocks make your whole post unreadable.

You can edit that post to change QUOTE and /QUOTE to CODE and /CODE.

Last edited by johnsfine; 02-12-2013 at 09:24 AM.
 
Old 02-12-2013, 09:37 AM   #4
tristandnv
LQ Newbie
 
Registered: Jan 2013
Location: Geneva
Posts: 6

Original Poster
Rep: Reputation: Disabled
The modifications you asked were made.

I found the error, I was in fact trying to access unallocated memory but the line you mentioned is ok, hitOverlap was initialized with 200 elements, and n in *(hitOverlap + n) is not running beyond that. The problem was here:
Code:
for(int ii = (int)(StartOverlap/4); ii <= (int)(*(inputPars + 3*(nHitsOverlap-1))) + (int)(lastWidth/4); ii++) *(hitOverlap + ii + 1 - (int)(StartOverlap/4)) = (vGain*(double)wave[ii] - vOffset)-pedestal;
because riseTime, peakTime, etc, were in ns and not in sample units, so to convert them into sample units I had to divide them by 4 (sampling time of oscilloscope is 4ns). I should've mentioned this information perhaps.

Thanks for your concern and sorry for the bad style of coding. I'm a kind of self-learner, if you have any website from which I could learn to code in a better style, just let me know.
Daniel
 
Old 02-12-2013, 09:58 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by tristandnv View Post
but the line you mentioned is ok,
I realized that a moment after posting and deleted that from my post to try to avoid confusing you. But it appears that is the moment in which you looked.

Quote:
sorry for the bad style of coding. I'm a kind of self-learner, if you have any website from which I could learn to code in a better style,
If this is C++ code, you might want to use std::vector instead of C arrays.

In any case, it ought to be possible to have a more obvious, reliable, and verifiable relationship among:
1) The size of array you actually need
2) The size of array you allocate
3) The size of array you use

When that is hard to accomplish, use of assert instructions is effective (assuming a debug build of your project is not too slow to test).

There are several places where my co workers have coded array references (like yours) with computed indexes with no locally obvious relationship limiting the index to the bounds of the array.
That style has been the cause of many difficult to diagnose bugs. I always remind them to use asserts when the reason the computed index is within bounds is not locally obvious.

Instead of
Code:
*(hitOverlap + ii + 1 - (int)(StartOverlap/4)) = (vGain*(double)wave[ii] - vOffset)-pedestal;
with an assert it would be
Code:
{
  assert(  ii + 1 - (int)(StartOverlap/4) < 200 );
  *(hitOverlap + ii + 1 - (int)(StartOverlap/4)) = (vGain*(double)wave[ii] - vOffset)-pedestal;
}
It is also bad style for that 200 to appear as a simple number in the code. It is better to make that a defined constant.

I think it is also confusing to use
Code:
*(hitOverlap + ii + 1 - (int)(StartOverlap/4))
instead of
Code:
hitOverlap[ ii + 1 - (int)(StartOverlap/4) ]
In C those two expressions have the same meaning. But the second is much more meaningful to a human reading your code.
 
Old 02-12-2013, 10:04 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3174Reputation: 3174Reputation: 3174Reputation: 3174Reputation: 3174Reputation: 3174Reputation: 3174Reputation: 3174Reputation: 3174Reputation: 3174Reputation: 3174
Echoing Johnsfine's sentiment here, I very strongly recommend: (a) that you use C++ code, and (b) that you use the storage-classes provided by that language instead of C-style memory allocation and pointer-references.

The C++ standard class-library is reason enough to use C++. These libraries consist of thoroughly debugged and optimized code, along with detectors that would have flagged problems like these. They allow you to express your problem at a much higher level of abstraction ... a "vector" ... and thereby minimize the amount of code peculiar to your own application that could be a contributing factor in a disastrous failure such as the one you just experienced. You merely use the class-library, in the proper and documented way, and happily ignore how it works, merely trusting that it does.

The gcc system can process many different languages, and once you go past the first few stages of the compiler the code-generation process is pretty much identical. So, you're not paying a penalty for using the full expressiveness of C++. (In fact, sometimes it's faster.)

Last edited by sundialsvcs; 02-12-2013 at 10:05 AM.
 
Old 02-13-2013, 02:32 AM   #7
tristandnv
LQ Newbie
 
Registered: Jan 2013
Location: Geneva
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for your suggestions, I'll follow your advices on right style coding.

To be honest, one of my most important concerns is the saving of memory. In that sense, this statement is important for me:
Quote:
So, you're not paying a penalty for using the full expressiveness of C++. (In fact, sometimes it's faster.)
I use huge data to run my programs -in Physics this is a really important issue, my 'huge data' is nothing compared to what is processed in big collaboration projects. For example, I have data directories with up to 10+8 binaries files. In this case, even a simple
Quote:
ls
cannot be used, and you need to code a little program with system calls to list files in a directory. Let alone when you process these files. So, my question is, using C++ standard class-library is faster and secure at the same time?

Thanks.
 
Old 02-13-2013, 07:37 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
I noticed something else:

Quote:
Originally Posted by tristandnv View Post
Code:
        fitFunction->TF1::~TF1();
It seems you are going to some extra effort to create a memory leak. Why didn't you use
Code:
delete fitFunction;
Quote:
Originally Posted by tristandnv View Post
So, my question is, using C++ standard class-library is faster and secure at the same time?
The C++ standard library usually has some overhead, both space and time. Using it instead of doing the same thing well yourself usually has performance cost, rather than performance benefit. In most cases that performance cost is trivial.

Consider
std::vector<double> outputPars(40);
vs
outputPars = new double[40];
each followed by many later references to outputPars[i]

The vector takes a trivial amount of extra time and space when you construct it. But that extra is a fixed amount, not proportional to the size. When you reference outputPars[i] there is no difference in memory use or speed between the vector and non vector version.

Your use of *(outputPars+i) is only valid in the non vector version. That is another reason to prefer outputPars[i]. If you change your mind about the definition of outputPars, you shouldn't need to change the syntax where it is used. I hope you understand there is never any performance benefit to the *(outputPars+i) syntax. In the non vector case, the compiler generates exactly the same code as for outputPars[i].

So when you know the size (40) of an array at allocation time, there is little difference between the vector and non vector versions. With vector, you pay a trivial cost to have the compiler, rather than you, remember to delete the storage when it goes out of scope.

If you don't know the size at the moment of allocation, then vector might have bigger advantages.

C++ can be faster than code that would be practical to write in C when you start coding templates well. In C you could get the same performance by coding many different versions of the same functions instead of coding a slightly slower more general version. But usually that isn't practical. With C++ templates you can often code a very general purpose function with all the overhead of that generality dealt with at compile time getting the performance of individual specific versions at run time.

For less skilled coders, even the standard C++ library can be more efficient than doing it yourself, because you might not do it as well as the authors of the standard library. Doing such things yourself has the inherent advantage that you have less generality to worry about and can get a design more focused on your current needs. But that is balanced against having less time and expertise to optimize the effort. I coded my own replacements for std::set and std::map because I could get a big performance and functional advantage from getting a better fit to my requirements. But most programmers inventing their own associative containers would get worse performance than simply using std::set and std::map.

If you know the size at allocation time, it is hard to get worse performance using C arrays than using vector. But if you start inventing your own dynamic length array with realloc or similar, you'll likely end up with worse performance than std::vector.

Last edited by johnsfine; 02-13-2013 at 08:05 AM.
 
Old 02-13-2013, 08:19 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,413

Rep: Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600Reputation: 1600
Quote:
Originally Posted by tristandnv View Post
In this case, even a simple ls cannot be used, and you need to code a little program with system calls to list files in a directory.
ls is a little program with system calls to list files in a directory. It sorts by default though, have you tried ls --sort=none?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
free(): invalid next size (fast) Error dns Programming 3 03-09-2012 03:41 AM
[SOLVED] Why do I get corrupted memory? Error: free(): invalid next size (fast) C++arl Programming 3 04-19-2011 08:22 AM
C++ -- glibc detected *** a.out: free*(): invalid next size (fast): Eikcuhc Programming 2 11-07-2010 02:16 AM
*** glibc detected *** free(): invalid next size (fast): 0x0804a1a8 *** bartonski Programming 1 03-08-2010 02:57 AM
grep -Ri <searchterm> * returns glibc detected *** free(): invalid next size (fast) abylin1 Linux - Newbie 3 05-14-2009 04:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration