Function to free buffer memory