I wrote a predicate used in a remove_if
call that deletes shared_ptr's of type StemmedSentence from an vector of sentences.
The predicate:
class EraseSentenceIf {
ArrayStemmedSnippet * m_ass;
public:
EraseSentenceIf(ArrayStemmedSnippet *ass)
: m_ass(ass) {
}
bool operator()(const std::shared_ptr<
ArrayStemmedSnippet::StemmedSentence>& s) {
std::shared_ptr<ArrayStemmedSnippet::StemmedSentence> tmp = s;
// --- set StemmedSentnce object in ArrayStemmedSnippet class
s->setParent(m_ass);
// --- if true delete this sentence)
if (s->trimStopWords()) {
tmp.reset();
return true;
}
return false;
}
};
The remove_if call:
EraseSentenceIf esi(this);
sentences.erase(
std::remove_if(
sentences.begin(), sentences.end(), esi),
sentences.end()
);
Declaration:
std::vector<shared_ptr<StemmedSentence> > sentences;
The construction of the sentences objects looks like this:
sentences.push_back(shared_ptr<StemmedSentence>(
new StemmedSentence(index, i - 1 )));
The code seems to run fine, valgrind / gdb does not moan. I just want to get sure that I handle the deletion (or release) of the shared_ptr in a correct way. Can somebody please confirm this? Maybe I can improve something or I overlooked an important point. Thanks for your comments!
tmp.reset();
only resetstmp
, not the object inside ofsentences
. \$\endgroup\$tmp
?! I can not uses
because the compiler sayserror: ‘class ArrayStemmedSnippet::StemmedSentence’ has no member named ‘reset’
, which I understand. So how do I clearly delete it? \$\endgroup\$operator()
const
, you could pass yourEraseSentenceIf
object as temporary instead of using theesi
variable. \$\endgroup\$