Based on your description above, it seems you are right. I didn't have the time for proper analysis of the code, but at a quick look, there are many places where memory allocation is done with `-1`, maybe that should be removed in all those places.
Did you test by removing the `-1` and it worked as expected?