Fixed a memory leak where buf was not removed