Fix a few memory leak bugs as pointed out in the Mathworks file exchange.