Allocation problem during changing histos names

Hi,

I create a tree to which I add in loop histograms. I name them with name of file I read earlier and value read from array I have and it works perfectly when I use commented code. But when I try to use code signed below with stars I got errors. It’s even more confusing because using code with stars, loop over threshold for ind=0 always works properly, for ind=1 I have error:

Analysis of Const Threshold with threshold = -0.05
Analysis of Const Threshold with threshold = -0.06

 *** Break *** segmentation violation

and for ind=2 I have another error:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

Even more confusing is that this errors does not change no matter what is the initial value of ind. Furthermore for ind>2 occurs second type of errors from above but not for every value - rather for every second value.

My code looks like this:

  //------ Create file to save data ---------------------------
  TFile *file1 = new TFile("../root_files/ConstThresholdResults.root", "update");
  Double_t dt1_2_1, dt3_4_1;

  //------ Loop over all thresholds -----------------------------
  for(Int_t ind=0; ind<threshold.size();ind++){
    std::cout<<"Analysis of Const Threshold with threshold = "<<threshold[ind]<<std::endl;

    //------ Remove tree to overwrite --------------------------
    std::string treename1 = "delay_"+delays[ind]+".root_thr_"+std::to_string(threshold[ind]);     //***

//std::string treename1 = strstr(argv[1],"delay");
//treename1 += "_thr_"+std::to_string(threshold[ind]);

    std::string to_remove_name1 =treename1;
    to_remove_name1+=";*";

Whole code to my source code you can find here:

Edit!!
Working solution


ROOT Version: 6.12
Platform: Linux Mint
Compiler: gcc


Are you sure you have passed the correct (number of) command line arguments? Your program should check that delays.size() == threshold.size() just after the extract_delays loop has completed. You have 11 thresholds, do you have 11 corresponding delays?

General comment: I would not recommend to remove things from a root file - I find it much clearer if nothing gets changed/removed from root files. Just add things if necessary. Then it is much easier to reason about the data flow.

To track down this kind of problem the best is to run the failing example under valgrind

valgrind --suppression=$ROOTSYS/etc/valgrind-root.supp ....
1 Like

Thank both of you for response, @behrenhoff you are right, I went through this code so many times but have never noticed that mistake. But advise about valgrind is priceless @pcanal.

Thanks

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