From 42632acdf44807d262f1e3af124fe08f7dbcb97f Mon Sep 17 00:00:00 2001 From: Paul Bignier Date: Mon, 13 Jun 2016 10:23:56 +0200 Subject: [PATCH] Fileio gateways: allocate vector on stack instead of heap * These items don't need to be allocated on the heap, it leads to memleaks and poor readability Change-Id: I38adf0417ac39e653725d1d14ad0376663170bc1 --- .../modules/fileio/sci_gateway/cpp/sci_mscanf.cpp | 69 +++++++++----------- .../modules/fileio/sci_gateway/cpp/sci_msscanf.cpp | 43 ++++++------ 2 files changed, 52 insertions(+), 60 deletions(-) diff --git a/scilab/modules/fileio/sci_gateway/cpp/sci_mscanf.cpp b/scilab/modules/fileio/sci_gateway/cpp/sci_mscanf.cpp index d719ddc..213afcb 100644 --- a/scilab/modules/fileio/sci_gateway/cpp/sci_mscanf.cpp +++ b/scilab/modules/fileio/sci_gateway/cpp/sci_mscanf.cpp @@ -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* pIT = new std::vector(); + std::vector 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()->isScalar() == false || in[0]->getAs()->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(in[0]->getAs()->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()->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()->getRows(); + int sizeOfString = pIT[0]->getAs()->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()->get(j)); + pString->set(i * sizeOfString + j, pIT[i]->getAs()->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()->getRows(); + int sizeOfDouble = pIT[0]->getAs()->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()->get(j)); + pDouble->set(i * sizeOfDouble + j, pIT[i]->getAs()->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* pITTemp = new std::vector(); - pITTemp->push_back((*pIT)[0]); + std::vector pITTemp = std::vector(); + 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()->getRows(); - int iCols = pITTemp->back()->getAs()->getCols(); + int iRows = pITTemp.back()->getAs()->getRows(); + int iCols = pITTemp.back()->getAs()->getCols(); int arrayOfType[2] = {iRows, iCols + 1}; types::String* pType = new types::String(2, arrayOfType); - for (int k = 0; k < pITTemp->back()->getAs()->getSize(); k++) + for (int k = 0; k < pITTemp.back()->getAs()->getSize(); k++) { - pType->set(k, pITTemp->back()->getAs()->get(k)); + pType->set(k, pITTemp.back()->getAs()->get(k)); } - for (int k = 0; k < (*pIT)[i]->getAs()->getSize(); k++) + for (int k = 0; k < pIT[i]->getAs()->getSize(); k++) { - pType->set(iRows * iCols + k, (*pIT)[i]->getAs()->get(k)); + pType->set(iRows * iCols + k, pIT[i]->getAs()->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()->getRows(); - int iCols = pITTemp->back()->getAs()->getCols(); + int iRows = pITTemp.back()->getAs()->getRows(); + int iCols = pITTemp.back()->getAs()->getCols(); int arrayOfType[2] = {iRows, iCols + 1}; types::Double* pType = new types::Double(2, arrayOfType); - pType->set(pITTemp->back()->getAs()->get()); - for (int k = 0; k < (*pIT)[i]->getAs()->getSize(); k++) + pType->set(pITTemp.back()->getAs()->get()); + for (int k = 0; k < pIT[i]->getAs()->getSize(); k++) { - pType->set(iRows * iCols + k, (*pIT)[i]->getAs()->get(k)); + pType->set(iRows * iCols + k, pIT[i]->getAs()->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; } /*--------------------------------------------------------------------------*/ diff --git a/scilab/modules/fileio/sci_gateway/cpp/sci_msscanf.cpp b/scilab/modules/fileio/sci_gateway/cpp/sci_msscanf.cpp index 11e65c0..974283a 100644 --- a/scilab/modules/fileio/sci_gateway/cpp/sci_msscanf.cpp +++ b/scilab/modules/fileio/sci_gateway/cpp/sci_msscanf.cpp @@ -220,75 +220,74 @@ types::Function::ReturnValue sci_msscanf(types::typed_list &in, int _iRetCount, break; default : { - std::vector* pITTemp = new std::vector(); - pITTemp->push_back(IT[0]); + std::vector pITTemp = std::vector(); + 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()->getRows(); - int iCols = pITTemp->back()->getAs()->getCols(); + int iRows = pITTemp.back()->getAs()->getRows(); + int iCols = pITTemp.back()->getAs()->getCols(); int arrayOfType[2] = {iRows, iCols + 1}; types::String* pType = new types::String(2, arrayOfType); - for (int k = 0; k < pITTemp->back()->getAs()->getSize(); k++) + for (int k = 0; k < pITTemp.back()->getAs()->getSize(); k++) { - pType->set(k, pITTemp->back()->getAs()->get(k)); + pType->set(k, pITTemp.back()->getAs()->get(k)); } for (int k = 0; k < IT[i]->getAs()->getSize(); k++) { pType->set(iRows * iCols + k, IT[i]->getAs()->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()->getRows(); - int iCols = pITTemp->back()->getAs()->getCols(); + int iRows = pITTemp.back()->getAs()->getRows(); + int iCols = pITTemp.back()->getAs()->getCols(); int arrayOfType[2] = {iRows, iCols + 1}; types::Double* pType = new types::Double(2, arrayOfType); - pType->set(pITTemp->back()->getAs()->get()); + pType->set(pITTemp.back()->getAs()->get()); for (int k = 0; k < IT[i]->getAs()->getSize(); k++) { pType->set(iRows * iCols + k, IT[i]->getAs()->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); } -- 1.7.9.5