fix segmentation fault for flushing the buffer