Fix encode memory leak