The program was not failing on the first realloc. It was tying up memory as the program repeated itself and eventually exhausting all memory. This is when it was crashing.
The program was not failing on the first realloc. It was tying up memory as the program repeated itself and eventually exhausting all memory. This is when it was crashing.
Valgrind has to hold onto the memory so that it (and you) can spot any "use-after-free" memory problems you may have.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Aside from Valgrind I am still leaking memory due to constant reallocation. I guess this is just not a valid approach for a large amount of memory reallocation. I think my strategy will be to just malloc a very large cache of 256k blocks at the start of the daemon and then link them together as needed for files rather than realloc the individual blocks. I would imagine this would solve the problem.