Fix some corner cases in ringbuffer and make unitary test have no leak