Remove memory leak in dataset encoding function