PIC32 stuck after long time
It turned out the dynamic memory was indeed the problem.
I ended up using the ringbuffer implementation documented here: http://www.piconomic.co.za/fwlib/ring__buffer_8c_source.html