Segmentation fault, std::bad_alloc

I had a program which ran great interactively. Now I have compiled it so that I can run the program non interactively on the high performance processing center at my university.There are all kinds of issues, so I made this test code to pinpoint the problems and work through them. I have tried a few versions of this code.

Upon running ./test > LOG0 I get the following error.

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

–LOG0–

acarnes@r14a-s31:testcode$ cat LOG0
starting test program
creating new Tstring
new TString created.
creating TFile with c_str in constructor
TFile with c_str in constructor has been created
creating TMVA::Factory

–test.cpp–

#include "TObject.h"
#include "TFile.h"
#include "TString.h"
#include "TMVA/Factory.h"
#include "TMVA/Tools.h"

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>

int main(int argc, char *argv[])
{

     std::cout << "starting test program" << std::endl;
     std::cout << "creating new Tstring" << std::endl;

     TString filename("test.root");

     std::cout << "new TString created." << std::endl;

     std::cout << "creating TFile with c_str in constructor" << std::endl;
     TFile* file  = new TFile("blah.root", "RECREATE");

     std::cout << "TFile with c_str in constructor has been created" << std::endl;

     std::cout << "creating TMVA::Factory" << std::endl;
 
     // Load TMVA Library
     TMVA::Tools::Instance();

    //TString weightname("weightname");
    //TString options("!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");

     TMVA::Factory* factory = new TMVA::Factory("weightname", file,"!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );
         
     //clean up
     delete factory;
     file->Close(); 

     std::cout << "TMVA::Factory created";  

     return 0;
} 

Then I tried to rewrite the code. Upon running version 2 below. I received the following error.


 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x0000003578899fc5 in waitpid () from /lib64/libc.so.6
#1  0x000000357883c331 in do_system () from /lib64/libc.so.6
#2  0x00002ad4cb526a96 in TUnixSystem::StackTrace ()
   from /apps/root/5.32.01/lib/libCore.so
#3  0x00002ad4cb52636c in TUnixSystem::DispatchSignals ()
   from /apps/root/5.32.01/lib/libCore.so
#4  <signal handler called>
#5  0x0000000000402091 in main ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x0000000000402091 in main ()
===========================================================


*** glibc detected *** ./test: double free or corruption (!prev): 0x0000000007019ce0 ***
======= Backtrace: =========
/lib64/libc.so.6[0x357887230f]
/lib64/libc.so.6(cfree+0x4b)[0x357887276b]
/apps/root/5.32.01/lib/libCore.so(_ZN8TStorage13ObjectDeallocEPv+0x68)[0x2ad4cb4890c8]
/apps/root/5.32.01/lib/libRIO.so(_ZN5TFileD0Ev+0x217)[0x2ad4cae5e2d7]
/apps/root/5.32.01/lib/libCore.so(_ZN11TCollection14GarbageCollectEP7TObject+0x84)[0x2ad4cb4cced4]
/apps/root/5.32.01/lib/libCore.so(_ZN5TList6DeleteEPKc+0x145)[0x2ad4cb4d31e5]
/apps/root/5.32.01/lib/libCore.so(_ZN5TROOTD1Ev+0xbf)[0x2ad4cb48270f]
/lib64/libc.so.6(__cxa_finalize+0x8e)[0x357883368e]
/apps/root/5.32.01/lib/libCore.so[0x2ad4cb432386]

Here is the output of the program.
–LOG0v2–

starting test program
creating new Tstring
new TString created.
creating TFile with c_str in constructor
TFile with c_str in constructor has been created
creating TMVA::Factory

–testv2.cpp–


#include "TObject.h"
#include "TFile.h"
#include "TString.h"
#include "TMVA/Factory.h"
#include "TMVA/Tools.h"

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>

int main(int argc, char *argv[])
{

     std::cout << "starting test program" << std::endl;
     std::cout << "creating new Tstring" << std::endl;

     TString filename("test.root");

     std::cout << "new TString created." << std::endl;

     std::cout << "creating TFile with c_str in constructor" << std::endl;
     TFile* file  = new TFile("blah.root", "RECREATE");

     std::cout << "TFile with c_str in constructor has been created" << std::endl;

     std::cout << "creating TMVA::Factory" << std::endl;
 
     // Load TMVA Library
     TMVA::Tools::Instance();

     TString weightname("weightname");
     TString options("!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");

     TMVA::Factory* factory = new TMVA::Factory(weightname, file, options );
         
     //clean up
     delete factory;
     file->Close(); 

     std::cout << "TMVA::Factory created";  

     return 0;
} 

Here is the last and final version I tried, which gave me the following errors.

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x0000003578899fc5 in waitpid () from /lib64/libc.so.6
#1  0x000000357883c331 in do_system () from /lib64/libc.so.6
#2  0x00002b8f3f16ca96 in TUnixSystem::StackTrace ()
   from /apps/root/5.32.01/lib/libCore.so
#3  0x00002b8f3f16c36c in TUnixSystem::DispatchSignals ()
   from /apps/root/5.32.01/lib/libCore.so
#4  <signal handler called>
#5  0x00002b8f40fe6551 in std::ostream::sentry::sentry ()
   from /apps/root/5.32.01/lib/libstdc++.so.6
#6  0x00002b8f40fe7f66 in std::__ostream_insert<char, std::char_traits<char> >
    () from /apps/root/5.32.01/lib/libstdc++.so.6
#7  0x00002b8f40fe825f in std::operator<< <std::char_traits<char> > ()
   from /apps/root/5.32.01/lib/libstdc++.so.6
#8  0x00002b8f3a78b1e6 in TMVA::Configurable::ParseOptions ()
   from /apps/root/5.32.01/lib/libTMVA.so
#9  0x00002b8f3a7c41a7 in TMVA::Factory::Factory ()
   from /apps/root/5.32.01/lib/libTMVA.so
#10 0x00000000004021fb in main ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00002b8f40fe6551 in std::ostream::sentry::sentry ()
   from /apps/root/5.32.01/lib/libstdc++.so.6
#6  0x00002b8f40fe7f66 in std::__ostream_insert<char, std::char_traits<char> >
    () from /apps/root/5.32.01/lib/libstdc++.so.6
#7  0x00002b8f40fe825f in std::operator<< <std::char_traits<char> > ()
   from /apps/root/5.32.01/lib/libstdc++.so.6
#8  0x00002b8f3a78b1e6 in TMVA::Configurable::ParseOptions ()
   from /apps/root/5.32.01/lib/libTMVA.so
#9  0x00002b8f3a7c41a7 in TMVA::Factory::Factory ()
   from /apps/root/5.32.01/lib/libTMVA.so
#10 0x00000000004021fb in main ()
===========================================================

Here is the output from the program.

–LOG0v3–

starting test program
creating new Tstring
new TString created.
creating TFile with c_str in constructor
TFile with c_str in constructor has been created
creating TMVA::Factory

Here is the code responsible.

–testv3.cpp–

#include "TObject.h"
#include "TFile.h"
#include "TString.h"
#include "TMVA/Factory.h"
#include "TMVA/Tools.h"

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>

int main(int argc, char *argv[])
{

     std::cout << "starting test program" << std::endl;
     std::cout << "creating new Tstring" << std::endl;

     TString filename("test.root");

     std::cout << "new TString created." << std::endl;

     std::cout << "creating TFile with c_str in constructor" << std::endl;
     TFile file("blah.root", "RECREATE");
   
     TFile* f = &file;
 
     if (file.IsZombie()) {
       std::cout << "Error opening file" << std::endl;
       exit(-1);
    }   
    

     std::cout << "TFile with c_str in constructor has been created" << std::endl;

     std::cout << "creating TMVA::Factory" << std::endl;
 
     // Load TMVA Library
     TMVA::Tools::Instance();

     //TString weightname("weightname");
     //TString options("!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification");

     TMVA::Factory* factory = new TMVA::Factory("weightname", f, "" );
         
     //clean up
     delete factory;
     f->Close(); 

     std::cout << "TMVA::Factory created";  

     return 0;
} 

Thanks in advance to anyone who can help.

Try maybe another ROOT version -> I have tried all your examples with ROOT 5.28 and 5.34 and there were no crashes (this doesn’t mean that they are correct, of course, but at least they do not die).
BTW. Don’t call your executables “test” -> this word is a shell built-in command (-> e.g. use “trial” instead).

Ok. This issue has been solved. Thank you for your help. Turns out the system was accessing some old libraries.