Fixed memory leak in the matlab wrapper