Fix memory leak and delete buffer files