fix unittests
cleaning up memory during the encoding process