From e5048cabb69b3e69db2be70fe8471546b3e80726 Mon Sep 17 00:00:00 2001 From: Thomas Heinemann Date: Fri, 7 Sep 2012 13:02:59 +0200 Subject: [PATCH] Closed memory leaks. --- src/dtmc/atomic_proposition.h | 2 +- src/mrmc-cpp.cpp | 2 ++ src/parser/read_tra_file.cpp | 16 +++++++++++----- src/sparse/static_sparse_matrix.h | 14 +++++++------- test/parser/read_tra_file_test.cpp | 1 + 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/dtmc/atomic_proposition.h b/src/dtmc/atomic_proposition.h index 60c5edcca..50f12fda3 100644 --- a/src/dtmc/atomic_proposition.h +++ b/src/dtmc/atomic_proposition.h @@ -33,7 +33,7 @@ class AtomicProposition { } ~AtomicProposition() { - delete node_array; + delete[] node_array; } bool hasNodeLabel(uint_fast32_t nodeId) { diff --git a/src/mrmc-cpp.cpp b/src/mrmc-cpp.cpp index 6308cd169..7c5730f45 100644 --- a/src/mrmc-cpp.cpp +++ b/src/mrmc-cpp.cpp @@ -62,6 +62,8 @@ int main(int argc, char* argv[]) { printf("\n"); } + delete ssm; + return 0; } diff --git a/src/parser/read_tra_file.cpp b/src/parser/read_tra_file.cpp index e7bfd8a9f..4fec19a43 100644 --- a/src/parser/read_tra_file.cpp +++ b/src/parser/read_tra_file.cpp @@ -29,7 +29,7 @@ namespace parser{ /*! * This method does the first pass through the .tra file and computes -* the number of non zero elements that are not diagonal elements, +* the number of non-zero elements that are not diagonal elements, * which correspondents to the number of transitions that are not * self-loops. * (Diagonal elements are treated in a special way). @@ -50,6 +50,7 @@ static uint_fast32_t make_first_pass(FILE* p) { if (fgets(s, 1024, p) != NULL) { if (sscanf( s, "STATES %d", &rows) == 0) { pantheios::log_WARNING(pantheios::integer(rows)); + (void)fclose(p); throw mrmc::exceptions::wrong_file_format(); } } @@ -57,6 +58,7 @@ static uint_fast32_t make_first_pass(FILE* p) { //Reading No. of transitions if (fgets(s, 1024, p) != NULL) { if (sscanf( s, "TRANSITIONS %d", &non_zero) == 0) { + (void)fclose(p); throw mrmc::exceptions::wrong_file_format(); } } @@ -68,6 +70,7 @@ static uint_fast32_t make_first_pass(FILE* p) { uint_fast32_t row=0, col=0; double val=0.0; if (sscanf( s, "%d%d%lf", &row, &col, &val ) != 3) { + (void)fclose(p); throw mrmc::exceptions::wrong_file_format(); } //Diagonal elements are not counted into the result! @@ -86,7 +89,7 @@ static uint_fast32_t make_first_pass(FILE* p) { */ sparse::StaticSparseMatrix * read_tra_file(const char * filename) { - FILE *p; + FILE *p = NULL; char s[1024]; uint_fast32_t rows, non_zero; sparse::StaticSparseMatrix *sp = NULL; @@ -105,6 +108,7 @@ sparse::StaticSparseMatrix * read_tra_file(const char * filename) { if (fgets(s, 1024, p) != NULL) { if (sscanf( s, "STATES %d", &rows) == 0) { pantheios::log_WARNING(pantheios::integer(rows)); + (void)fclose(p); throw mrmc::exceptions::wrong_file_format(); } } @@ -116,6 +120,7 @@ sparse::StaticSparseMatrix * read_tra_file(const char * filename) { if (fgets(s, 1024, p) != NULL) { uint_fast32_t nnz=0; if (sscanf( s, "TRANSITIONS %d", &nnz) == 0) { + (void)fclose(p); throw mrmc::exceptions::wrong_file_format(); } } @@ -124,9 +129,8 @@ sparse::StaticSparseMatrix * read_tra_file(const char * filename) { pantheios::integer(rows), " rows and ", pantheios::integer(non_zero), " Non-Zero-Elements"); /* Creating matrix - * Variable non_zero does NOT count any diagonal element, - * But all diagonal elements are allocated, so the number of allocated - * elements is non_zero + * Memory for diagonal elements is automatically allocated, hence only the number of non-diagonal + * non-zero elements has to be specified (which is non_zero, computed by make_first_pass) */ sp = new sparse::StaticSparseMatrix(rows,non_zero); sp->initialize(); @@ -141,6 +145,8 @@ sparse::StaticSparseMatrix * read_tra_file(const char * filename) { uint_fast32_t row=0, col=0; double val = 0.0; if (sscanf( s, "%d%d%lf", &row, &col, &val) != 3) { + delete sp; + (void)fclose(p); throw mrmc::exceptions::wrong_file_format(); } pantheios::log_DEBUG("Write value ", diff --git a/src/sparse/static_sparse_matrix.h b/src/sparse/static_sparse_matrix.h index 3fc68a7fa..6393d0d2d 100644 --- a/src/sparse/static_sparse_matrix.h +++ b/src/sparse/static_sparse_matrix.h @@ -19,7 +19,7 @@ namespace sparse { //! A sparse Matrix for DTMCs with a constant number of non-zero entries on the non-diagonal fields. /*! The sparse matrix used for calculation on the DTMCs. - Adressing is NOT zero-based! The valid range for getValue and addNextValue is 1 to rows (first argument to constructor). + Addressing is NOT zero-based! The valid range for getValue and addNextValue is 1 to rows (first argument to constructor). */ template class StaticSparseMatrix { @@ -27,7 +27,7 @@ class StaticSparseMatrix { //! Constructor /*! - \param rows Row-Count and therefor column-count of the symetric matrix + \param rows Row-Count and therefore column-count of the symmetric matrix \param non_zero_entries The exact count of entries that will be submitted through addNextValue *excluding* those on the diagonal (A_{i,j} with i = j) */ StaticSparseMatrix(uint_fast32_t rows, uint_fast32_t non_zero_entries) { @@ -48,19 +48,19 @@ class StaticSparseMatrix { ~StaticSparseMatrix() { if (value_storage != NULL) { //free(value_storage); - delete value_storage; + delete[] value_storage; } if (column_indications != NULL) { //free(column_indications); - delete column_indications; + delete[] column_indications; } if (row_indications != NULL) { //free(row_indications); - delete row_indications; + delete[] row_indications; } if (diagonal_storage != NULL) { //free(diagonal_storage); - delete diagonal_storage; + delete[] diagonal_storage; } } @@ -198,7 +198,7 @@ class StaticSparseMatrix { /*! Array containing the column number of the corresponding value_storage entry */ uint_fast32_t* column_indications; - /*! Array containing the row boundarys of valueStorage */ + /*! Array containing the row boundaries of valueStorage */ uint_fast32_t* row_indications; }; diff --git a/test/parser/read_tra_file_test.cpp b/test/parser/read_tra_file_test.cpp index 253f17ea7..39b501bca 100644 --- a/test/parser/read_tra_file_test.cpp +++ b/test/parser/read_tra_file_test.cpp @@ -59,6 +59,7 @@ TEST(ReadTraFileTest, ParseFileTest1) { ASSERT_TRUE(result->getValue(4,4,&val)); ASSERT_EQ(val,0); + delete result; } TEST(ReadTraFileTest, WrongFormatTestHeader1) {