fix memory freeing bug (wrong api usage)