[memleak] scicos => var2vec leak fixed about List and Struct 22/17322/4
Cedric Delamarre [Tue, 13 Oct 2015 15:28:31 +0000 (17:28 +0200)]
a = var2vec(tlist(["" ""],list()));
vec2var(a);

a = var2vec(struct("toto", list(struct("titi", 12))));
vec2var(a);

test_run scicos var2vec
test_run scicos vec2var

Change-Id: I288c779e4d773bb80f82407e766a77fd82d09683

scilab/modules/scicos/src/cpp/vec2var.cpp
scilab/modules/scicos/tests/unit_tests/var2vec.dia.ref
scilab/modules/scicos/tests/unit_tests/var2vec.tst

index 5a2028f..c4e54b6 100644 (file)
@@ -408,6 +408,7 @@ static bool readElement(const double* const input, const int iType, const int iD
                 offsetSave = offset;
                 offset = 2;
             }
+
             for (int i = 0; i < iDims; ++i)
             {
                 if (inputRows < 2 + offset)
@@ -425,7 +426,12 @@ static bool readElement(const double* const input, const int iType, const int iD
                     delete pList;
                     return false;
                 }
+
                 pList->append(element);
+                if (element->isList())
+                {
+                    element->killMe();
+                }
             }
             offset += offsetSave;
             res = pList;
@@ -447,8 +453,6 @@ static bool readElement(const double* const input, const int iType, const int iD
                 break;
             }
 
-            types::Struct* pStruct = new types::Struct(1, 1);
-
             int offsetSave = 0;
             if (offset == 0)
             {
@@ -473,6 +477,8 @@ static bool readElement(const double* const input, const int iType, const int iD
             {
                 return false;
             }
+
+            types::Struct* pStruct = new types::Struct(1, 1);
             types::String* header = element->getAs<types::String>();
             // ... and copy it in 'pStruct'
             for (int i = 0; i < header->getSize(); ++i)
@@ -484,6 +490,7 @@ static bool readElement(const double* const input, const int iType, const int iD
             {
                 if (inputRows < 2 + offset)
                 {
+                    delete pStruct;
                     Scierror(999, _("%s: Wrong size for input argument #%d: At least %dx%d expected.\n"), vec2varName.c_str(), 1, offset + 2, 1);
                     return false;
                 }
@@ -492,10 +499,13 @@ static bool readElement(const double* const input, const int iType, const int iD
                 elementDims = static_cast<int>(*(input + offset + 1));
                 if (!readElement(input + offset, elementType, elementDims, inputRows - offset, offset, element))
                 {
+                    delete pStruct;
                     return false;
                 }
                 pStruct->get(0)->set(header->get(i - 1), element);
             }
+
+            header->killMe();
             offset += offsetSave;
             res = pStruct;
             break;
index 7e8ff53..b389605 100644 (file)
@@ -138,3 +138,8 @@ assert_checkequal(vec2var(var2vec(exprs)), exprs);
 // List containing a Struct
 lStFull = list(stFull, [], stFull, "test");
 assert_checkequal(vec2var(var2vec(lStFull)), lStFull);
+// corner-cases
+a = var2vec(tlist(["" ""],list()));
+vec2var(a);
+a = var2vec(struct("toto", list(struct("titi", 12))));
+vec2var(a);
index 7dd4734..33f6191 100644 (file)
@@ -151,3 +151,11 @@ assert_checkequal(vec2var(var2vec(exprs)), exprs);
 // List containing a Struct
 lStFull = list(stFull, [], stFull, "test");
 assert_checkequal(vec2var(var2vec(lStFull)), lStFull);
+
+// corner-cases
+a = var2vec(tlist(["" ""],list()));
+vec2var(a);
+
+a = var2vec(struct("toto", list(struct("titi", 12))));
+vec2var(a);
+