Terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad

In the loop of my program, when the loop variable bin=8, I have this error:

terminate called after throwing an instance of 'std::bad_alloc'   what():  std::bad

 and the program stops.

Here is a part of my code:

    for(int bin=0; bin<sn; bin++){
        if(S[j]<=s1+0.05 && S[j]>=s1-0.05){
        if(S[j]<=s2+0.05 && S[j]>=s2-0.05){
    double x1=-b1/n; 
    double x2=-b2/n; 
    double x3=140;       
    double x4=140;
    double y3=(n*x3)+b2; 
    double y4=(n*x4)+b1;
    double y11=0.0;
    double y2=0.0;
    double  h[5]={x1,x2,x3,x4,x1};
    double k[5]={y11,y2,y3,y4,y11};
    cut[bin]=new TCutG(cha[1],5,h,k);
    for (Long64_t jentry=evSta; jentry<g; jentry++)
        if (cut[bin]->IsInside(nEout,nEin)){

Dear hajar,

In the part of the code that you posted, nothing points to a problem with ROOT.
If you think that this is the case, could you please provide a complete reproducer inside the ROOT shell?
Please, specify also the version of your ROOT installation.

G Ganis

Dear hajar,

Both your Ubuntu version and ROOT version are old and now unsupported. You should try to upgrade asap.

The error that you get indicates a memory leak. Your code is too complex to understand where it happens,
but you are calling things in loops (for example Analysis) and you have to make sure that all objects created with ‘new’ are properly deleted. I do not see any ‘delete’ call in the code.

G Ganis

Dear hajar,

Well, in C++, objects created with new need to be deleted with delete when not required anymore, otherwise they continue to use memory and eventually prevent proper running.
For example, in histo() you create new histograms:

hp[bin]=new TH2F(cha[1],cha[2],140, 0, 140, 140,0, 140);

to be properly cleaned-up this needs, at some point, a call to

delete hp[bin];

G Ganis

1 Like

You should use delete hp[bin] . The Delete operator is for special purposes and should not be used; see TObject::Delete .

G Ganis

Thanks for your answer I used

but I have this problem so;
terminate called after throwing an instance of ‘std::bad_alloc’ what(): std::bad

If it is not the unbalanced new/deletes, my best guess would be that you are writing out of bounds somewhere.

What makes it hard to reason about your program:

  • There are way too many global variables. Your goal should be 0, zero, none at all; you have already more than I can count!
  • Way too many hardcoded numbers in your code, you never know if the arrays have the correct size
  • There is no checking for the number of lines in the input files you are reading. You are assuming that your array is large enough.
  • in C++ use constants, not #defines
  • all your functions are way too long. Keep then short (goal: max 1x screen height). Otherwise you can’t see what the code is doing.
  • keep the scope of your variables as short as possible

Given all these general comments, my recommendation would be to rewrite the code. That’s probably not what you want, so three different suggestions that might help on the short run:
a) use a static analyzer like cppcheck or clang-tidy to spot bugs (I can’t do it without the pdbreakup.h file)
b) compile this program using a “real” compiler (you need to add some missing headers) and use the built-in runtime checks like -fsanitize=address (or memory/undefined).
c) comment out some parts of the script and check if the problem still exists. Repeat until you find the bug.

1 Like

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