Fix buffer leak on encoding exception