Fix memory issue with toMat