Index: /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp	(revision 12036)
+++ /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp	(revision 12037)
@@ -33,5 +33,5 @@
 		#endif
 
-		/*Now transpose the matrix: */
+		/*Now transpose the matrix and allocate with Matlab's memory manager: */
 		tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
 		for(i=0;i<cols;i++){
@@ -63,4 +63,5 @@
 	mxArray* dataref=NULL;
 	double*  vector_ptr=NULL;
+	double*  vector_ptr2=NULL;
 	int      rows;
 	
@@ -74,15 +75,20 @@
 		#endif
 		
-		/*now create the matlab vector: */
+		/*now create the matlab vector with Matlab's memory manager */
+		vector_ptr2=(double*)mxMalloc(rows*sizeof(double));
+		for(int i=0;i<rows;i++) vector_ptr2[i]=vector_ptr[i];
+
 		dataref = mxCreateDoubleMatrix(0,0,mxREAL);                         
 		mxSetM(dataref,rows);
 		mxSetN(dataref,1);                                                                                          
-		mxSetPr(dataref,vector_ptr);           
+		mxSetPr(dataref,vector_ptr2);           
 	}
 	else{
 		dataref = mxCreateDoubleMatrix(0,0,mxREAL);
 	}
+
+	/*Clean-up and return*/
+	xfree((void**)&vector_ptr);
 	*pdataref=dataref;
-
 }
 /*}}}*/
