C++ program dies with std::bad_alloc, BUT valgrind reports no memory leaks
c++, memory, valgrind
Solution
Run the program under debugger so that it stops once that exception is thrown and you can observe the call stack.
Three most probable problems are:
- heap fragmentation
- too many objects created on heap (but still pointed to from the program)
- a request for an unreasonably large block of memory
Problem
My program fails with 'std::bad_alloc' error message. The program is scalable, so I've tested on a smaller version with valgrind and there are no memory leaks. This is an application of statistical mechanics, so I am basically making hundreds of objects, changing their internal data (in this case stl vectors of doubles), and writing to a datafile. The creation of objects lies inside a loop, so when it ends the memory is free. Something like: ``` for (cont=0;cont<MAX;cont++){ classSection seccion; seccion.GenerateObjects(...); while(somecondition){ seccion.evolve(); seccion.writedatatofile(); }} ``` So there are two variables which set the computing time of the program, the size of the system and the number of runs. There is only crash for big systems with many runs. Any ideas on how to catch this memory problem? Thanks,