repaired memory leak

This commit is contained in:
Rafał Jaworski 2019-02-28 20:56:22 +01:00
parent 7f2a212c6a
commit ce400eb185
5 changed files with 27 additions and 32 deletions

View File

@ -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<HashGenerator>(
new HashGenerator(_indexPath,
_config));
_T = boost::shared_ptr<std::vector<sauchar_t> >(
new std::vector<sauchar_t>);
_T = boost::shared_ptr<std::vector<sauchar_t> >(new std::vector<sauchar_t>);
_SA = boost::shared_ptr<std::vector<saidx_t> >(new std::vector<saidx_t>);
_markers = boost::shared_ptr<std::vector<SUFFIX_MARKER_TYPE> >(
new std::vector<SUFFIX_MARKER_TYPE>);
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<std::vector<saidx_t> >(
new std::vector<saidx_t>);
} else {
throw ConcordiaException("Index corrupt: missing files");
}
}

View File

@ -18,21 +18,16 @@ ConcordiaIndex::ConcordiaIndex(const std::string & hashedIndexFilePath,
ConcordiaIndex::~ConcordiaIndex() {
}
boost::shared_ptr<std::vector<saidx_t> > ConcordiaIndex::generateSuffixArray(
boost::shared_ptr<std::vector<sauchar_t> > 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<std::vector<sauchar_t> > T,
boost::shared_ptr<std::vector<saidx_t> > SA) {
SA->clear();
for(int i=0;i<T->size();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<std::vector<saidx_t> > result =
boost::shared_ptr<std::vector<saidx_t> >(new std::vector<saidx_t>);
for (int i = 0; i < T->size(); i++) {
result->push_back(SA_array[i]);
}
delete[] SA_array;
return result;
}
std::vector<TokenizedSentence> ConcordiaIndex::addAllExamples(

View File

@ -116,8 +116,9 @@ public:
/*! Generates suffix array based on the passed hashed index.
\returns the generated suffix array
*/
boost::shared_ptr<std::vector<saidx_t> > generateSuffixArray(
boost::shared_ptr<std::vector<sauchar_t> > T);
void generateSuffixArray(
boost::shared_ptr<std::vector<sauchar_t> > T,
boost::shared_ptr<std::vector<saidx_t> > SA);
private:
void _addSingleTokenizedExample(

View File

@ -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<std::vector<saidx_t> > SA = index.generateSuffixArray(T);
boost::shared_ptr<std::vector<saidx_t> > SA(new std::vector<saidx_t>());
index.generateSuffixArray(T,SA);
boost::shared_ptr<std::vector<saidx_t> > expectedSA = boost::shared_ptr<std::vector<saidx_t> >(new std::vector<saidx_t>());
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<std::vector<saidx_t> > SA = index.generateSuffixArray(T);
boost::shared_ptr<std::vector<saidx_t> > SA(new std::vector<saidx_t>());
index.generateSuffixArray(T,SA);
boost::shared_ptr<std::vector<saidx_t> > expectedSA = boost::shared_ptr<std::vector<saidx_t> >(new std::vector<saidx_t>());
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<std::vector<saidx_t> > SA = index.generateSuffixArray(T);
boost::shared_ptr<std::vector<saidx_t> > SA(new std::vector<saidx_t>());
index.generateSuffixArray(T,SA);
boost::shared_ptr<std::vector<saidx_t> > expectedSA = boost::shared_ptr<std::vector<saidx_t> >(new std::vector<saidx_t>());
expectedSA->push_back(0);

View File

@ -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<std::vector<saidx_t> > SA = index.generateSuffixArray(T);
boost::shared_ptr<std::vector<saidx_t> > SA(new std::vector<saidx_t>());
index.generateSuffixArray(T,SA);
// searching for pattern "Ola posiada rysia Marysia" (5 1 3 4)