Fixed memory freeing issue during main encoding/decoding finish