Fileio gateways: allocate vector on stack instead of heap 39/18239/2
Paul Bignier [Mon, 13 Jun 2016 08:23:56 +0000 (10:23 +0200)]
 * These items don't need to be allocated on the heap, it leads to memleaks and poor readability

Change-Id: I38adf0417ac39e653725d1d14ad0376663170bc1

scilab/modules/fileio/sci_gateway/cpp/sci_mscanf.cpp
scilab/modules/fileio/sci_gateway/cpp/sci_msscanf.cpp

index d719ddc..213afcb 100644 (file)
@@ -42,7 +42,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
     wchar_t* wcsFormat          = NULL;
     wchar_t* wcsRead            = NULL;
     int dimsArray[2]            = {1, 1};
-    std::vector<types::InternalType*>* pIT = new std::vector<types::InternalType*>();
+    std::vector<types::InternalType*> pIT;
 
     int args        = 0;
     int nrow        = 0;
@@ -58,7 +58,6 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
     if (size < 1 || size > 2)
     {
         Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "mscanf", 1, 2);
-        delete pIT;
         return types::Function::Error;
     }
 
@@ -67,7 +66,6 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         if (in[0]->isDouble() == false || in[0]->getAs<types::Double>()->isScalar() == false || in[0]->getAs<types::Double>()->isComplex())
         {
             Scierror(999, _("%s: Wrong type for input argument #%d: A Real expected.\n"), "mscanf", 1);
-            delete pIT;
             return types::Function::Error;
         }
         iNiter = static_cast<int>(in[0]->getAs<types::Double>()->get(0));
@@ -80,7 +78,6 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
     if (in[size - 1]->isString() == false || in[size - 1]->getAs<types::String>()->isScalar() == false)
     {
         Scierror(999, _("%s: Wrong type for input argument #%d: A String expected.\n"), "mscanf", size);
-        delete pIT;
         return types::Function::Error;
     }
 
@@ -115,7 +112,6 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         FREE(wcsRead);
         if (err < 0)
         {
-            delete pIT;
             return types::Function::Error;
         }
         err = Store_Scan(&nrow, &ncol, type_s, type, &retval, &retval_s, buf, &data, rowcount, args);
@@ -149,7 +145,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
                 {
                     ps->set(j, data[i + ncol * j].s);
                 }
-                pIT->push_back(ps);
+                pIT.push_back(ps);
                 uiFormatUsed |= (1 << 1);
             }
             break;
@@ -167,7 +163,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
                 {
                     p->set(j, data[i + ncol * j].d);
                 }
-                pIT->push_back(p);
+                pIT.push_back(p);
                 uiFormatUsed |= (1 << 2);
             }
             break;
@@ -176,7 +172,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         }
     }
 
-    int sizeOfVector = (int)pIT->size();
+    int sizeOfVector = (int)pIT.size();
     if (_iRetCount > 1)
     {
         types::Double* pDouble = new types::Double(2, dimsArray);
@@ -185,7 +181,7 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
 
         for (int i = 0; i < sizeOfVector; i++)
         {
-            out.push_back((*pIT)[i]);
+            out.push_back(pIT[i]);
         }
         for (int i = sizeOfVector + 1; i < _iRetCount; i++)
         {
@@ -197,7 +193,6 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         if (sizeOfVector == 0)
         {
             out.push_back(new types::String(L""));
-            delete pIT;
             return types::Function::OK;
         }
 
@@ -205,14 +200,14 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
         {
             case (1 << 1) :
             {
-                int sizeOfString = (*pIT)[0]->getAs<types::String>()->getRows();
+                int sizeOfString = pIT[0]->getAs<types::String>()->getRows();
                 int dimsArrayOfRes[2] = {sizeOfString, sizeOfVector};
                 types::String* pString = new types::String(2, dimsArrayOfRes);
                 for (int i = 0; i < sizeOfVector; i++)
                 {
                     for (int j = 0; j < sizeOfString; j++)
                     {
-                        pString->set(i * sizeOfString + j, (*pIT)[i]->getAs<types::String>()->get(j));
+                        pString->set(i * sizeOfString + j, pIT[i]->getAs<types::String>()->get(j));
                     }
                 }
                 out.push_back(pString);
@@ -220,14 +215,14 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
             break;
             case (1 << 2) :
             {
-                int sizeOfDouble = (*pIT)[0]->getAs<types::Double>()->getRows();
+                int sizeOfDouble = pIT[0]->getAs<types::Double>()->getRows();
                 int dimsArrayOfRes[2] = {sizeOfDouble, sizeOfVector};
                 types::Double* pDouble = new types::Double(2, dimsArrayOfRes);
                 for (int i = 0; i < sizeOfVector; i++)
                 {
                     for (int j = 0; j < sizeOfDouble; j++)
                     {
-                        pDouble->set(i * sizeOfDouble + j, (*pIT)[i]->getAs<types::Double>()->get(j));
+                        pDouble->set(i * sizeOfDouble + j, pIT[i]->getAs<types::Double>()->get(j));
                     }
                 }
                 out.push_back(pDouble);
@@ -235,74 +230,72 @@ types::Function::ReturnValue sci_mscanf(types::typed_list &in, int _iRetCount, t
             break;
             default :
             {
-                std::vector<types::InternalType*>* pITTemp = new std::vector<types::InternalType*>();
-                pITTemp->push_back((*pIT)[0]);
+                std::vector<types::InternalType*> pITTemp = std::vector<types::InternalType*>();
+                pITTemp.push_back(pIT[0]);
 
                 // sizeOfVector always > 1
                 for (int i = 1; i < sizeOfVector; i++) // concatenates the Cells. ex : [String 4x1] [String 4x1] = [String 4x2]
                 {
-                    if (pITTemp->back()->getType() == (*pIT)[i]->getType())
+                    if (pITTemp.back()->getType() == pIT[i]->getType())
                     {
-                        switch (pITTemp->back()->getType())
+                        switch (pITTemp.back()->getType())
                         {
                             case types::InternalType::ScilabString :
                             {
-                                int iRows               = pITTemp->back()->getAs<types::String>()->getRows();
-                                int iCols               = pITTemp->back()->getAs<types::String>()->getCols();
+                                int iRows               = pITTemp.back()->getAs<types::String>()->getRows();
+                                int iCols               = pITTemp.back()->getAs<types::String>()->getCols();
                                 int arrayOfType[2]      = {iRows, iCols + 1};
                                 types::String* pType    = new types::String(2, arrayOfType);
 
-                                for (int k = 0; k < pITTemp->back()->getAs<types::String>()->getSize(); k++)
+                                for (int k = 0; k < pITTemp.back()->getAs<types::String>()->getSize(); k++)
                                 {
-                                    pType->set(k, pITTemp->back()->getAs<types::String>()->get(k));
+                                    pType->set(k, pITTemp.back()->getAs<types::String>()->get(k));
                                 }
-                                for (int k = 0; k < (*pIT)[i]->getAs<types::String>()->getSize(); k++)
+                                for (int k = 0; k < pIT[i]->getAs<types::String>()->getSize(); k++)
                                 {
-                                    pType->set(iRows * iCols + k, (*pIT)[i]->getAs<types::String>()->get(k));
+                                    pType->set(iRows * iCols + k, pIT[i]->getAs<types::String>()->get(k));
                                 }
-                                pITTemp->pop_back();
-                                pITTemp->push_back(pType);
+                                pITTemp.pop_back();
+                                pITTemp.push_back(pType);
                             }
                             break;
                             case types::InternalType::ScilabDouble :
                             {
-                                int iRows               = pITTemp->back()->getAs<types::Double>()->getRows();
-                                int iCols               = pITTemp->back()->getAs<types::Double>()->getCols();
+                                int iRows               = pITTemp.back()->getAs<types::Double>()->getRows();
+                                int iCols               = pITTemp.back()->getAs<types::Double>()->getCols();
                                 int arrayOfType[2]      = {iRows, iCols + 1};
                                 types::Double* pType    = new types::Double(2, arrayOfType);
 
-                                pType->set(pITTemp->back()->getAs<types::Double>()->get());
-                                for (int k = 0; k < (*pIT)[i]->getAs<types::Double>()->getSize(); k++)
+                                pType->set(pITTemp.back()->getAs<types::Double>()->get());
+                                for (int k = 0; k < pIT[i]->getAs<types::Double>()->getSize(); k++)
                                 {
-                                    pType->set(iRows * iCols + k, (*pIT)[i]->getAs<types::Double>()->get(k));
+                                    pType->set(iRows * iCols + k, pIT[i]->getAs<types::Double>()->get(k));
                                 }
-                                pITTemp->pop_back();
-                                pITTemp->push_back(pType);
+                                pITTemp.back();
+                                pITTemp.push_back(pType);
                             }
                             break;
                             default :
-                                delete pITTemp;
                                 return types::Function::Error;
                         }
                     }
                     else
                     {
-                        pITTemp->push_back((*pIT)[i]);
+                        pITTemp.push_back(pIT[i]);
                     }
                 }
 
-                int dimsArrayOfCell[2] = {1, (int)pITTemp->size()};
+                int dimsArrayOfCell[2] = {1, (int)pITTemp.size()};
                 types::Cell* pCell = new types::Cell(2, dimsArrayOfCell);
                 for (int i = 0; i < dimsArrayOfCell[1]; i++)
                 {
-                    pCell->set(i, (*pITTemp)[i]);
+                    pCell->set(i, pITTemp[i]);
                 }
                 out.push_back(pCell);
             }
         }
     }
     Free_Scan(rowcount, ncol, type_s, &data);
-    delete pIT;
     return types::Function::OK;
 }
 /*--------------------------------------------------------------------------*/
index 11e65c0..974283a 100644 (file)
@@ -220,75 +220,74 @@ types::Function::ReturnValue sci_msscanf(types::typed_list &in, int _iRetCount,
             break;
             default :
             {
-                std::vector<types::InternalType*>* pITTemp = new std::vector<types::InternalType*>();
-                pITTemp->push_back(IT[0]);
+                std::vector<types::InternalType*> pITTemp = std::vector<types::InternalType*>();
+                pITTemp.push_back(IT[0]);
 
                 // sizeOfVector always > 1
                 for (int i = 1; i < sizeOfVector; i++) // concatenates the Cells. ex : [String 4x1] [String 4x1] = [String 4x2]
                 {
-                    if (pITTemp->back()->getType() == IT[i]->getType())
+                    if (pITTemp.back()->getType() == IT[i]->getType())
                     {
-                        switch (pITTemp->back()->getType())
+                        switch (pITTemp.back()->getType())
                         {
                             case types::InternalType::ScilabString :
                             {
-                                int iRows               = pITTemp->back()->getAs<types::String>()->getRows();
-                                int iCols               = pITTemp->back()->getAs<types::String>()->getCols();
+                                int iRows               = pITTemp.back()->getAs<types::String>()->getRows();
+                                int iCols               = pITTemp.back()->getAs<types::String>()->getCols();
                                 int arrayOfType[2]      = {iRows, iCols + 1};
                                 types::String* pType    = new types::String(2, arrayOfType);
 
-                                for (int k = 0; k < pITTemp->back()->getAs<types::String>()->getSize(); k++)
+                                for (int k = 0; k < pITTemp.back()->getAs<types::String>()->getSize(); k++)
                                 {
-                                    pType->set(k, pITTemp->back()->getAs<types::String>()->get(k));
+                                    pType->set(k, pITTemp.back()->getAs<types::String>()->get(k));
                                 }
                                 for (int k = 0; k < IT[i]->getAs<types::String>()->getSize(); k++)
                                 {
                                     pType->set(iRows * iCols + k, IT[i]->getAs<types::String>()->get(k));
                                 }
-                                pITTemp->pop_back();
-                                pITTemp->push_back(pType);
+                                pITTemp.pop_back();
+                                pITTemp.push_back(pType);
                             }
                             break;
                             case types::InternalType::ScilabDouble :
                             {
-                                int iRows               = pITTemp->back()->getAs<types::Double>()->getRows();
-                                int iCols               = pITTemp->back()->getAs<types::Double>()->getCols();
+                                int iRows               = pITTemp.back()->getAs<types::Double>()->getRows();
+                                int iCols               = pITTemp.back()->getAs<types::Double>()->getCols();
                                 int arrayOfType[2]      = {iRows, iCols + 1};
                                 types::Double* pType    = new types::Double(2, arrayOfType);
 
-                                pType->set(pITTemp->back()->getAs<types::Double>()->get());
+                                pType->set(pITTemp.back()->getAs<types::Double>()->get());
                                 for (int k = 0; k < IT[i]->getAs<types::Double>()->getSize(); k++)
                                 {
                                     pType->set(iRows * iCols + k, IT[i]->getAs<types::Double>()->get(k));
                                 }
-                                pITTemp->pop_back();
-                                pITTemp->push_back(pType);
+                                pITTemp.pop_back();
+                                pITTemp.push_back(pType);
                             }
                             break;
                             default :
-                                delete pITTemp;
                                 return types::Function::Error;
                         }
                     }
                     else
                     {
-                        pITTemp->push_back(IT[i]);
+                        pITTemp.push_back(IT[i]);
                     }
                 }
 
                 types::MList* pMList = new types::MList();
                 pMList->append(new types::String(L"cblock"));
-                for (int i = 0 ; i < pITTemp->size() ; i++)
+                for (int i = 0 ; i < pITTemp.size() ; i++)
                 {
-                    pMList->append((*pITTemp)[i]);
+                    pMList->append(pITTemp[i]);
                 }
                 out.push_back(pMList);
 
-                //                int dimsArrayOfCell[2] = {1, (int)pITTemp->size()};
+                //                int dimsArrayOfCell[2] = {1, (int)pITTemp.size()};
                 //                types::Cell* pCell = new types::Cell(2, dimsArrayOfCell);
-                //                for (int i = 0; i < pITTemp->size(); i++)
+                //                for (int i = 0; i < pITTemp.size(); i++)
                 //                {
-                //                    pCell->set(i, (*pITTemp)[i]);
+                //                    pCell->set(i, pITTemp[i]);
                 //                }
                 //                out.push_back(pCell);
             }