Crashes in Vector of vectors with Root 5_34_patches

Hello,
I am working with a vector of vector of vectors containing integers with ROOT v5_34, and problems are met in this piece of code.

int hLSF (char *fpath, int nchans, int chMin,  int chMax) {
	char fname[1000];
	const int npos = 5;//100;
	char file_pref[5] = "/th_";
	char specifier[3] = "%d";
	char ext[7] = "_0.raw";
	int th_begin=350;
	int th_end= 400;//455;
	int th_interval=5;
	const int nthresh = ((th_end-th_begin)/th_interval) +1;
	
	Double_t count_at_threshold; 		
        vector<vector<vector<int>>> vec_th(npos, vector<vector<int>> (chMax));	
	
	for(int ipos=0; ipos<npos+1; ipos++) {
		sprintf(fname,"%s%d%s%s%s", fpath, ipos,file_pref, specifier,ext);		
		TH2F *h2_dummy=createScan(fname, th_begin, th_end, th_interval, nchans);
	
		for (int ichan = chMin; ichan<chMax; ichan++){	
			TH1F *h1_ichan = getCh(h2_dummy, ichan);
			count_at_threshold=0; 
			for(int ithresh=th_begin; ithresh<th_end+th_interval; ithresh=ithresh+th_interval) {
			   count_at_threshold = h1_ichan->GetBinContent(h1_ichan->FindBin(ithresh)); 				
			  vec_th[ipos]ichan].push_back(count_at_threshold);												
			}
			delete h1_ichan;										
		}								
		delete h2_dummy;
	}		
	return 0;
}

and with this crash log

*** Break *** segmentation violation



===========================================================
There was a crash (#6 0x00a44fdd in SigHandler(ESignals) ()).
This is the entire stack trace of all threads:
===========================================================
#0  0x004a5402 in __kernel_vsyscall ()
#1  0x00593cb3 in __waitpid_nocancel () from /lib/libc.so.6
#2  0x0053862b in do_system () from /lib/libc.so.6
#3  0x00a407dd in TUnixSystem::Exec(char const*) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#4  0x00a485cd in TUnixSystem::StackTrace() ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#5  0x00a44f0b in TUnixSystem::DispatchSignals(ESignals) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#6  0x00a44fdd in SigHandler(ESignals) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#7  0x00a3dbf4 in sighandler(int) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#8  0x00a6fae1 in textinput::TerminalConfigUnix::HandleSignal(int) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#9  0x00a6ff46 in (anonymous namespace)::TerminalConfigUnix__handleSignal(int)
    () from /users/root_v5-34-00-patches/lib/libCore.so
#10 <signal handler called>
#11 0x00d3ac7c in G__G__Cont_183_0_23(G__value*, char const*, G__param*, int)
    () from /users/root_v5-34-00-patches/lib/libCore.so
#12 0x00ff7eda in Cint::G__ExceptionWrapper(int (*)(G__value*, char const*, G__param*, int), G__value*, char*, G__param*, int) ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#13 0x0100d9b8 in G__exec_asm ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#14 0x010ee32f in G__exec_loop(char const*, char*, std::list<G__FastAllocString, std::allocator<G__FastAllocString> > const&) ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#15 0x010e59ee in G__exec_statement ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#16 0x0108ae86 in G__interpret_func ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#17 0x01077478 in G__getfunction ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#18 0x0104b4a1 in G__getitem ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#19 0x01054d69 in G__getexpr ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#20 0x010e8e4f in G__exec_statement ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#21 0x01035b3b in G__exec_tempfile_core ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#22 0x01035e96 in G__exec_tempfile_fp ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#23 0x010f2ed8 in G__process_cmd ()
   from /users/root_v5-34-00-patches/lib/libCint.so
#24 0x009fa613 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#25 0x0093c5ee in TApplication::ProcessLine(char const*, bool, int*) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#26 0x0011c4e7 in TRint::HandleTermInput() ()
   from /users/root_v5-34-00-patches/lib/libRint.so
#27 0x0011b6a5 in TTermInputHandler::Notify() ()
   from /users/root_v5-34-00-patches/lib/libRint.so
#28 0x0011e7b4 in TTermInputHandler::ReadNotify() ()
   from /users/root_v5-34-00-patches/lib/libRint.so
#29 0x00a44aeb in TUnixSystem::CheckDescriptors() ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#30 0x00a45675 in TUnixSystem::DispatchOneEvent(bool) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#31 0x009a88a4 in TSystem::InnerLoop() ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#32 0x009ad0b1 in TSystem::Run() ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#33 0x0093a828 in TApplication::Run(bool) ()
   from /users/root_v5-34-00-patches/lib/libCore.so
#34 0x0011e328 in TRint::Run(bool) ()
   from /users/root_v5-34-00-patches/lib/libRint.so
#35 0x08048d43 in main ()
===========================================================


Root > Function hLSF() busy flag cleared

Thanks in advance for the help

Try to pre-compile your code (fix any reported errors and warnings).
Instead of std::vector::operator[] use std::vector::at (i.e. instead of “SomeVector[i][j]” use “SomeVector.at(i).at(j)”).

Hi fclopez,

if you want help it would be helpful if you could provide some actual code. The code you posted contains numerous syntax errors and I doubt even ancient CINT would parse this.

Especially when having the kind of issue you are having now it helps the switch to a more defensive programming style:

  • avoid pointers, use plain objects instead
  • avoid char[] and const char* in favor for std::string
  • use std::vector in place of C-arrays, with checked access with vec.at(i) instead of vec[i] like already suggested by Wile
  • use an actual compiler to check your code for obvious mistakes
  • run valgrind on your compiled code to catch more subtle mistakes

In addition some basic working knowledge of C++ also pays back in the long run.