don't free buffer before we use it
fix mem leaks