diff --git a/testing/testing_zgeadd.c b/testing/testing_zgeadd.c
index cf65e1843e1ae2f9b52d263ee77ca34630cbb1e2..b4caefa8a4b5b19971389299cbe7c72f95dfd368 100644
--- a/testing/testing_zgeadd.c
+++ b/testing/testing_zgeadd.c
@@ -83,7 +83,10 @@ int testing_zgeadd(int argc, char **argv)
     MORSE_Complex64_t *Bfinal = (MORSE_Complex64_t *)malloc(LDBxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ((!A)||(!B)||(!Binit)||(!Bfinal)){
+    if ( (!A) || (!B) || (!Binit) || (!Bfinal) )
+    {
+        free(A); free(B);
+        free(Binit); free(Bfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zgels.c b/testing/testing_zgels.c
index 1d2f248325593d6d08ced5e920f07db771bdaa3f..4b52193e7abe497f50fd8ca462975f5f21f0dea4 100644
--- a/testing/testing_zgels.c
+++ b/testing/testing_zgels.c
@@ -258,7 +258,11 @@ int testing_zgels(int argc, char **argv)
     MORSE_desc_t *T;
 
     /* Check if unable to allocate memory */
-    if ((!A1)||(!A2)||(!B1)||(!B2)||(!Q)){
+    if ( (!A1) || (!A2) || (!B1) || (!B2) || (!Q) )
+    {
+        free(A1); free(A2);
+        free(B1); free(B2);
+        free(Q);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zgels_hqr.c b/testing/testing_zgels_hqr.c
index 4f216e0d60514b68803d0cd0533e12550f537a3d..d3f8fde83c298e0286e7d4bd2b23d48b9242b651 100644
--- a/testing/testing_zgels_hqr.c
+++ b/testing/testing_zgels_hqr.c
@@ -95,7 +95,11 @@ int testing_zgels_hqr(int argc, char **argv)
     MORSE_desc_t *TT = NULL;
 
     /* Check if unable to allocate memory */
-    if ((!A1)||(!A2)||(!B1)||(!B2)||(!Q)){
+    if ( (!A1) || (!A2) || (!B1) || (!B2) || (!Q) )
+    {
+        free(A1); free(A2);
+        free(B1); free(B2);
+        free(Q);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zgels_systolic.c b/testing/testing_zgels_systolic.c
index 85c89caebaf143aebfef80bd086157567bb551e1..52e7e46d731923fb1d02f9f770583637e80ee4fd 100644
--- a/testing/testing_zgels_systolic.c
+++ b/testing/testing_zgels_systolic.c
@@ -89,7 +89,11 @@ int testing_zgels_systolic(int argc, char **argv)
     MORSE_desc_t *TT = NULL;
 
     /* Check if unable to allocate memory */
-    if ((!A1)||(!A2)||(!B1)||(!B2)||(!Q)){
+    if ( (!A1) || (!A2) || (!B1) || (!B2) || (!Q) )
+    {
+        free(A1); free(A2);
+        free(B1); free(B2);
+        free(Q);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zgemm.c b/testing/testing_zgemm.c
index 562134b796265f72c41fc3ac17f400af9d169a7f..e10da200aa8b7c11714304c45a7da55d55db7a1b 100644
--- a/testing/testing_zgemm.c
+++ b/testing/testing_zgemm.c
@@ -88,7 +88,10 @@ int testing_zgemm(int argc, char **argv)
     MORSE_Complex64_t *Cfinal = (MORSE_Complex64_t *)malloc(LDCxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ((!A)||(!B)||(!Cinit)||(!Cfinal)){
+    if ( (!A) || (!B) || (!Cinit) || (!Cfinal) )
+    {
+        free(A); free(B);
+        free(Cinit); free(Cfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zgeqrf_qdwh.c b/testing/testing_zgeqrf_qdwh.c
index cd03ec4337a5fa184e0e2f8308d53d3f587d0379..21bce4fa612d9c967060ff83f70ffbf6bd7dfc99 100644
--- a/testing/testing_zgeqrf_qdwh.c
+++ b/testing/testing_zgeqrf_qdwh.c
@@ -87,6 +87,8 @@ int testing_zgeqrf_qdwh(int argc, char **argv)
 
     /* Check if unable to allocate memory */
     if ( (!A) || (!A1) || (!A2) || (!Q1) || (!Q2) ){
+        free(A); free(A1); free(A2);
+        free(Q1); free(Q2);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zgesv_incpiv.c b/testing/testing_zgesv_incpiv.c
index c89a97e2d09e5791e4ef5ce35394703f7dfe8ded..0b063dbc81a81c11398895e032ed844321e99d0c 100644
--- a/testing/testing_zgesv_incpiv.c
+++ b/testing/testing_zgesv_incpiv.c
@@ -196,7 +196,10 @@ int testing_zgesv_incpiv(int argc, char **argv)
     int *IPIV;
 
     /* Check if unable to allocate memory */
-    if ( (!A1) || (!A2)|| (!B1) || (!B2) ) {
+    if ( (!A1) || (!A2)|| (!B1) || (!B2) )
+    {
+        free(A1); free(A2);
+        free(B1); free(B2);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zgesvd.c b/testing/testing_zgesvd.c
index e83248e941f949aa8022ee78ce7783c0d6d392b0..0bca121e37d4c91517399b405e9d9666cbb6a955 100644
--- a/testing/testing_zgesvd.c
+++ b/testing/testing_zgesvd.c
@@ -100,6 +100,8 @@ int testing_zgesvd(int argc, char **argv)
 
     /* Check if unable to allocate memory */
     if ( (!A1) || (!S1) || (!S2) || (!work) ) {
+        free(A1); free(work);
+        free(S1); free(S2);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zheevd.c b/testing/testing_zheevd.c
index cdc66adf453e4f015040edca3b43578e5f558e2e..2466e5b79b5531acbf9aa97fd0619ffaf3fb7d5e 100644
--- a/testing/testing_zheevd.c
+++ b/testing/testing_zheevd.c
@@ -72,7 +72,11 @@ int testing_zheevd(int argc, char **argv)
     MORSE_desc_t      *T;
 
     /* Check if unable to allocate memory */
-    if ( (!A2) || (!W1) || (!W2) ){
+    if ( (!A2) || (!W1) || (!W2) || !(work) )
+    {
+        free(A2);
+        free(W1); free(W2);
+        free(work);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zhemm.c b/testing/testing_zhemm.c
index 53910f9e25ed84a15dda15b1d7f509fff4dc16b5..891b4314e1ff8a58464e07a47dc8815e7ad46d16 100644
--- a/testing/testing_zhemm.c
+++ b/testing/testing_zhemm.c
@@ -81,7 +81,10 @@ int testing_zhemm(int argc, char **argv)
     MORSE_Complex64_t *Cfinal = (MORSE_Complex64_t *)malloc(LDCxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ((!A)||(!B)||(!Cinit)||(!Cfinal)){
+    if ( (!A) || (!B) || (!C) || (!Cinit) || (!Cfinal) )
+    {
+        free(A); free(B); free(C);
+        free(Cinit); free(Cfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zher2k.c b/testing/testing_zher2k.c
index 7276353b11f552be567d0d0332d490a1e0f3cfbe..d15366eb2cb92ec5cdcd6979bbc5af16ec2005e5 100644
--- a/testing/testing_zher2k.c
+++ b/testing/testing_zher2k.c
@@ -82,7 +82,9 @@ int testing_zher2k(int argc, char **argv)
     MORSE_Complex64_t *Cfinal = (MORSE_Complex64_t *)malloc(LDCxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ( (!A) || (!B) || (!Cinit) || (!Cfinal) ){
+    if ( (!A) || (!B) || (!C) || (!Cinit) || (!Cfinal) ){
+        free(A); free(B); free(C);
+        free(Cinit); free(Cfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zherk.c b/testing/testing_zherk.c
index a218be22f157ccf81a2146d57a813a828e268df4..21a5824c05b9306dfc6d2812b230e5bf51116c42 100644
--- a/testing/testing_zherk.c
+++ b/testing/testing_zherk.c
@@ -77,7 +77,9 @@ int testing_zherk(int argc, char **argv)
     MORSE_Complex64_t *Cfinal = (MORSE_Complex64_t *)malloc(LDCxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ( (!A) || (!Cinit) || (!Cfinal) ){
+    if ( (!A) || (!C) || (!Cinit) || (!Cfinal) ){
+        free(A); free(C);
+        free(Cinit); free(Cfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zpemv.c b/testing/testing_zpemv.c
index 401eea1ee15d178baa3d2a7cd9d5e544c7892915..ef70a9236d23d7b7eb9495fd41051640a48d514a 100644
--- a/testing/testing_zpemv.c
+++ b/testing/testing_zpemv.c
@@ -134,9 +134,12 @@ int testing_zpemv(int argc, char **argv)
     LAPACKE_zlarnv_work(1, ISEED, 1, &beta0 );
 
     /* Check if unable to allocate memory */
-    if ( (!A) || (!X) || (!Y0) || (!work) ) {
+    if ( (!A) || (!A0) || (!X) || (!Y) || (!Y0) || (!work) ) {
+        free(A); free(A0);
+        free(X); free(Y); free(Y0);
+        free(work);
         printf("Out of Memory \n ");
-        exit(0);
+        return -2;
     }
 
     /* Initialize Data */
diff --git a/testing/testing_zposv.c b/testing/testing_zposv.c
index 0b4906c4d15f748e54fde7cd381dafd946f51de0..a129e6a599412dde76c3ac20d276ac4bd2cd480d 100644
--- a/testing/testing_zposv.c
+++ b/testing/testing_zposv.c
@@ -189,13 +189,16 @@ int testing_zposv(int argc, char **argv)
     int info_solution, info_factorization;
     int trans1, trans2;
 
-    MORSE_Complex64_t *A1   = (MORSE_Complex64_t *)malloc(LDA*N*sizeof(MORSE_Complex64_t));
-    MORSE_Complex64_t *A2   = (MORSE_Complex64_t *)malloc(LDA*N*sizeof(MORSE_Complex64_t));
-    MORSE_Complex64_t *B1   = (MORSE_Complex64_t *)malloc(LDB*NRHS*sizeof(MORSE_Complex64_t));
-    MORSE_Complex64_t *B2   = (MORSE_Complex64_t *)malloc(LDB*NRHS*sizeof(MORSE_Complex64_t));
+    MORSE_Complex64_t *A1 = (MORSE_Complex64_t *)malloc(LDA*N*sizeof(MORSE_Complex64_t));
+    MORSE_Complex64_t *A2 = (MORSE_Complex64_t *)malloc(LDA*N*sizeof(MORSE_Complex64_t));
+    MORSE_Complex64_t *B1 = (MORSE_Complex64_t *)malloc(LDB*NRHS*sizeof(MORSE_Complex64_t));
+    MORSE_Complex64_t *B2 = (MORSE_Complex64_t *)malloc(LDB*NRHS*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ((!A1)||(!A2)||(!B1)||(!B2)){
+    if ( (!A1) || (!A2)|| (!B1) || (!B2) )
+    {
+        free(A1); free(A2);
+        free(B1); free(B2);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zpotri.c b/testing/testing_zpotri.c
index 2dff3ea05836051ee4bd4f5166ab7ccc9e551114..6b5b1beeb58c4ab4ea034e580aef4ceb555c10dc 100644
--- a/testing/testing_zpotri.c
+++ b/testing/testing_zpotri.c
@@ -199,7 +199,10 @@ int testing_zpotri(int argc, char **argv)
     MORSE_Complex64_t *WORK = (MORSE_Complex64_t *)malloc(2*LDA*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ((!A1)||(!A2)){
+    if ( (!A1) || (!A2) || (!work) )
+    {
+        free(A1); free(A2);
+        free(work);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zsymm.c b/testing/testing_zsymm.c
index 0e037c761b86b7f5b92d49ceb400a725ecaaedac..cecc6b83850abc18bf6e577d46c98d3aa7232e41 100644
--- a/testing/testing_zsymm.c
+++ b/testing/testing_zsymm.c
@@ -81,7 +81,10 @@ int testing_zsymm(int argc, char **argv)
     MORSE_Complex64_t *Cfinal = (MORSE_Complex64_t *)malloc(LDCxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ((!A)||(!B)||(!Cinit)||(!Cfinal)){
+    if ( (!A) || (!B) || (!C) || (!Cinit) || (!Cfinal) )
+    {
+        free(A); free(B); free(C);
+        free(Cinit); free(Cfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zsyr2k.c b/testing/testing_zsyr2k.c
index cbd5a75d4ec8de376763c7728ad82dac96c0a8ee..049e9c8fd98d9097958c0086076377c99eb8ea6c 100644
--- a/testing/testing_zsyr2k.c
+++ b/testing/testing_zsyr2k.c
@@ -82,7 +82,10 @@ int testing_zsyr2k(int argc, char **argv)
     MORSE_Complex64_t *Cfinal = (MORSE_Complex64_t *)malloc(LDCxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ( (!A) || (!B) || (!Cinit) || (!Cfinal) ){
+    if ( (!A) || (!B) || (!C) || (!Cinit) || (!Cfinal) )
+    {
+        free(A); free(B); free(C);
+        free(Cinit); free(Cfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_zsyrk.c b/testing/testing_zsyrk.c
index ba58ac727974fe1573113771848116137195b407..b942d294d8aa750cc262ea8d87837075d3e9f3d2 100644
--- a/testing/testing_zsyrk.c
+++ b/testing/testing_zsyrk.c
@@ -77,7 +77,9 @@ int testing_zsyrk(int argc, char **argv)
     MORSE_Complex64_t *Cfinal = (MORSE_Complex64_t *)malloc(LDCxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ( (!A) || (!Cinit) || (!Cfinal) ){
+    if ( (!A) || (!C) || (!Cinit) || (!Cfinal) ){
+        free(A); free(C);
+        free(Cinit); free(Cfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_ztrmm.c b/testing/testing_ztrmm.c
index 83214ef269c94da86fdd2a7543713cfe8655bfc5..f663a88277009497d2fa63a5102cc76682fdde47 100644
--- a/testing/testing_ztrmm.c
+++ b/testing/testing_ztrmm.c
@@ -77,7 +77,10 @@ int testing_ztrmm(int argc, char **argv)
     MORSE_Complex64_t *Bfinal = (MORSE_Complex64_t *)malloc(LDBxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ( (!A) || (!B) || (!Binit) || (!Bfinal)){
+    if ( (!A) || (!B) || (!Binit) || (!Bfinal) )
+    {
+        free(A); free(B);
+        free(Binit); free(Bfinal);
         printf("Out of Memory \n ");
         return -2;
     }
diff --git a/testing/testing_ztrsm.c b/testing/testing_ztrsm.c
index 3514b78d9691d6f6eb9a53ca00aff6b9bde18767..65e5088d604b62d29b6c8abf70a2ab03d30be786 100644
--- a/testing/testing_ztrsm.c
+++ b/testing/testing_ztrsm.c
@@ -77,7 +77,10 @@ int testing_ztrsm(int argc, char **argv)
     MORSE_Complex64_t *Bfinal = (MORSE_Complex64_t *)malloc(LDBxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ( (!A) || (!B) || (!Binit) || (!Bfinal)){
+    if ( (!A) || (!B) || (!Binit) || (!Bfinal) )
+    {
+        free(A); free(B);
+        free(Binit); free(Bfinal);
         printf("Out of Memory \n ");
         return -2;
     }