Mitch Wheat already suggested this in a comment, but I think it is in fact the best answer for your problem, so:
Use valgrind.
Valgrind is a tool (or rather, a set of tools) for which the default mode of operation is to run your program and check for memory errors, such as leaks, buffer overruns, uninitialized reads, and more.
Literally all you need to do is to build your program, preferably with the -g
option to make it easier to debug, and then run it like valgrind my-prog args...
. Valgrind will then print out detailed error reports with stack traces when something bad is going on. Your problem here will likely be found without ever needing to use a regular debugger, nor have us guess at the problem (if this were my own problem, I'd use valgrind too).