Coverity: graphic_export module resource leaks fixed
[scilab.git] / scilab / modules / graphic_export / src / cpp / xs2file.cpp
index 05adfd4..dcfb48d 100644 (file)
@@ -188,8 +188,8 @@ int xs2file(char * fname, ExportFileType fileType, void* pvApiCtx)
 
                     if ((!checkInputArgumentType(pvApiCtx, 3, sci_strings)))
                     {
-                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         Scierror(999, _("%s: Wrong type for input argument #%d: Single character string expected.\n"), fname, 3);
+                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         return 1;
                     }
 
@@ -197,14 +197,15 @@ int xs2file(char * fname, ExportFileType fileType, void* pvApiCtx)
                     if (sciErr.iErr)
                     {
                         printError(&sciErr, 0);
+                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         return 1;
                     }
 
                     // Retrieve a matrix of string at position 3.
                     if (getAllocatedMatrixOfString(pvApiCtx, piAddrsciOrientation, &nbRow, &nbCol, &sciOrientation))
                     {
-                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         Scierror(202, _("%s: Wrong type for argument #%d: string expected.\n"), fname, 3);
+                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         return 1;
                     }
 
@@ -224,17 +225,17 @@ int xs2file(char * fname, ExportFileType fileType, void* pvApiCtx)
                         }
                         else
                         {
+                            Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 3, "portrait", "landscape");
                             freeAllocatedMatrixOfString(m1, n1, fileName);
                             freeAllocatedMatrixOfString(nbRow, nbCol, sciOrientation);
-                            Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), fname, 3, "portrait", "landscape");
                             return 1;
                         }
                     }
                     else
                     {
+                        Scierror(999, _("%s: Wrong size for input argument #%d: Single character string expected.\n"), fname, 3);
                         freeAllocatedMatrixOfString(m1, n1, fileName);
                         freeAllocatedMatrixOfString(nbRow, nbCol, sciOrientation);
-                        Scierror(999, _("%s: Wrong size for input argument #%d: Single character string expected.\n"), fname, 3);
                         return 1;
                     }
                 }
@@ -244,6 +245,7 @@ int xs2file(char * fname, ExportFileType fileType, void* pvApiCtx)
                     if (sciErr.iErr)
                     {
                         printError(&sciErr, 0);
+                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         return 1;
                     }
 
@@ -251,16 +253,16 @@ int xs2file(char * fname, ExportFileType fileType, void* pvApiCtx)
                     sciErr = getMatrixOfDouble(pvApiCtx, piAddrquality, &nbRow, &nbCol, &quality);
                     if (sciErr.iErr)
                     {
-                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         printError(&sciErr, 0);
                         Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 3);
+                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         return 1;
                     }
 
                     if (nbRow != 1 || nbCol != 1 || *quality < 0 || *quality > 1)
                     {
-                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         Scierror(999, _("%s: Wrong type for input argument #%d: A real between 0 and 1 expected.\n"), fname, 3);
+                        freeAllocatedMatrixOfString(m1, n1, fileName);
                         return 1;
                     }
                     jpegCompressionQuality = (float) * quality;
@@ -269,6 +271,7 @@ int xs2file(char * fname, ExportFileType fileType, void* pvApiCtx)
 
             /* Replaces SCI, ~, HOME, TMPDIR by the real path */
             real_filename = expandPathVariable(fileName[0]);
+            freeAllocatedMatrixOfString(m1, n1, fileName);
 
             /* Call the function for exporting file */
             status = exportToFile(figureUID, real_filename, fileType, jpegCompressionQuality, orientation);
@@ -279,7 +282,6 @@ int xs2file(char * fname, ExportFileType fileType, void* pvApiCtx)
                 FREE(real_filename);
                 real_filename = NULL;
             }
-            freeAllocatedMatrixOfString(m1, n1, fileName);
 
             /* treat errors */
             if (strlen(status) != 0)
@@ -293,8 +295,8 @@ int xs2file(char * fname, ExportFileType fileType, void* pvApiCtx)
         }
         else
         {
-            freeAllocatedMatrixOfString(m1, n1, fileName);
             Scierror(999, _("%s: Wrong size for input argument #%d: Single character string expected.\n"), fname, 2);
+            freeAllocatedMatrixOfString(m1, n1, fileName);
             return 1;
         }
     }