From ce400eb185e1ae667162ea6297d11b15862d3fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Jaworski?= Date: Thu, 28 Feb 2019 20:56:22 +0100 Subject: [PATCH] repaired memory leak --- concordia/concordia.cpp | 20 +++++++------------- concordia/concordia_index.cpp | 21 ++++++++------------- concordia/concordia_index.hpp | 5 +++-- concordia/t/test_concordia_index.cpp | 9 ++++++--- concordia/t/test_concordia_searcher.cpp | 4 +++- 5 files changed, 27 insertions(+), 32 deletions(-) diff --git a/concordia/concordia.cpp b/concordia/concordia.cpp index 783de43..0fad639 100644 --- a/concordia/concordia.cpp +++ b/concordia/concordia.cpp @@ -161,7 +161,7 @@ void Concordia::loadRAMIndexFromDisk() { throw ConcordiaException("Index corrupt: empty markers file"); } // generating suffix array - _SA = _index->generateSuffixArray(_T); + _index->generateSuffixArray(_T,_SA); } catch (const std::bad_alloc&) { throw ConcordiaException("Error allocating memory, probably out of memory."); } @@ -171,7 +171,7 @@ void Concordia::loadRAMIndexFromDisk() { } void Concordia::refreshSAfromRAM() { - _SA = _index->generateSuffixArray(_T); + _index->generateSuffixArray(_T,_SA); } @@ -179,20 +179,14 @@ void Concordia::_initializeIndex() { _hashGenerator = boost::shared_ptr( new HashGenerator(_indexPath, _config)); - _T = boost::shared_ptr >( - new std::vector); + _T = boost::shared_ptr >(new std::vector); + _SA = boost::shared_ptr >(new std::vector); _markers = boost::shared_ptr >( new std::vector); - if (boost::filesystem::exists(_getWordMapFilePath()) - && boost::filesystem::exists(_getHashedIndexFilePath())) { + if ( boost::filesystem::exists(_getWordMapFilePath()) + && boost::filesystem::exists(_getHashedIndexFilePath()) + && boost::filesystem::exists(_getMarkersFilePath())) { loadRAMIndexFromDisk(); - } else if (!boost::filesystem::exists(_getWordMapFilePath()) - && !boost::filesystem::exists(_getHashedIndexFilePath())) { - // empty index - _SA = boost::shared_ptr >( - new std::vector); - } else { - throw ConcordiaException("Index corrupt: missing files"); } } diff --git a/concordia/concordia_index.cpp b/concordia/concordia_index.cpp index 09f98ee..f1538f7 100644 --- a/concordia/concordia_index.cpp +++ b/concordia/concordia_index.cpp @@ -18,21 +18,16 @@ ConcordiaIndex::ConcordiaIndex(const std::string & hashedIndexFilePath, ConcordiaIndex::~ConcordiaIndex() { } -boost::shared_ptr > ConcordiaIndex::generateSuffixArray( - boost::shared_ptr > T) { - saidx_t * SA_array = new saidx_t[T->size()]; - if (divsufsort(T->data(), SA_array, (saidx_t) T->size()) != 0) { +void ConcordiaIndex::generateSuffixArray( + boost::shared_ptr > T, + boost::shared_ptr > SA) { + SA->clear(); + for(int i=0;isize();i++) { + SA->push_back(0); + } + if (divsufsort(T->data(), SA->data(), (saidx_t) T->size()) != 0) { throw ConcordiaException("Error creating suffix array."); } - - boost::shared_ptr > result = - boost::shared_ptr >(new std::vector); - for (int i = 0; i < T->size(); i++) { - result->push_back(SA_array[i]); - } - - delete[] SA_array; - return result; } std::vector ConcordiaIndex::addAllExamples( diff --git a/concordia/concordia_index.hpp b/concordia/concordia_index.hpp index 3e84438..ee101e5 100644 --- a/concordia/concordia_index.hpp +++ b/concordia/concordia_index.hpp @@ -116,8 +116,9 @@ public: /*! Generates suffix array based on the passed hashed index. \returns the generated suffix array */ - boost::shared_ptr > generateSuffixArray( - boost::shared_ptr > T); + void generateSuffixArray( + boost::shared_ptr > T, + boost::shared_ptr > SA); private: void _addSingleTokenizedExample( diff --git a/concordia/t/test_concordia_index.cpp b/concordia/t/test_concordia_index.cpp index bcbcc35..d264b35 100644 --- a/concordia/t/test_concordia_index.cpp +++ b/concordia/t/test_concordia_index.cpp @@ -32,7 +32,8 @@ BOOST_AUTO_TEST_CASE( SuffixArrayGenerationTest ) // n: 0 1 2 3 4 5 6 7 8 //SA[n]: 0 3 1 7 4 2 8 5 6 - boost::shared_ptr > SA = index.generateSuffixArray(T); + boost::shared_ptr > SA(new std::vector()); + index.generateSuffixArray(T,SA); boost::shared_ptr > expectedSA = boost::shared_ptr >(new std::vector()); expectedSA->push_back(0); @@ -73,7 +74,8 @@ BOOST_AUTO_TEST_CASE( SuffixArrayGenerationTest2 ) // n: 0 1 2 3 4 5 6 7 8 9 10 11 //SA[n]: 0 4 1 9 5 2 10 6 8 11 3 7 - boost::shared_ptr > SA = index.generateSuffixArray(T); + boost::shared_ptr > SA(new std::vector()); + index.generateSuffixArray(T,SA); boost::shared_ptr > expectedSA = boost::shared_ptr >(new std::vector()); expectedSA->push_back(0); @@ -111,7 +113,8 @@ BOOST_AUTO_TEST_CASE( SuffixArrayGenerationTest3 ) // n: 0 1 2 3 4 5 //SA[n]: 5 3 1 0 4 2 - boost::shared_ptr > SA = index.generateSuffixArray(T); + boost::shared_ptr > SA(new std::vector()); + index.generateSuffixArray(T,SA); boost::shared_ptr > expectedSA = boost::shared_ptr >(new std::vector()); expectedSA->push_back(0); diff --git a/concordia/t/test_concordia_searcher.cpp b/concordia/t/test_concordia_searcher.cpp index 16dc68d..953c9aa 100644 --- a/concordia/t/test_concordia_searcher.cpp +++ b/concordia/t/test_concordia_searcher.cpp @@ -331,6 +331,7 @@ BOOST_AUTO_TEST_CASE( LcpSearch1 ) } + BOOST_AUTO_TEST_CASE( TmMatchesTest ) { ConcordiaSearcher searcher; @@ -369,7 +370,8 @@ BOOST_AUTO_TEST_CASE( TmMatchesTest ) index.addExample(hashGenerator, T, markers, Example("Ala posiada rysia",51)); index.addExample(hashGenerator, T, markers, Example("Marysia posiada rysia",123)); - boost::shared_ptr > SA = index.generateSuffixArray(T); + boost::shared_ptr > SA(new std::vector()); + index.generateSuffixArray(T,SA); // searching for pattern "Ola posiada rysia Marysia" (5 1 3 4)