I am trying to sort a vector of smart pointers to a class. I use a struct as the third parameter to std::sort
with operator()
:
struct PhraseSmartPtrParseCreationComparer
{
bool operator()(const std::shared_ptr<Phrase>& a, const std::shared_ptr<Phrase>& b)
{
if (a.get() == nullptr)
return b.get() != nullptr;
if (b.get() == nullptr)
return a.get() != nullptr;
return *a < *b;
}
};
Once in a while, I get a segmentation fault where one of the pointers inside the comparing method points to an invalid structure; always one. The interesting bit is, just before the sort, all the objects are intact; I also tried modifying the function to remove the reference bit: const std::shared_ptr<Phrase> a
, and it crashed elsewhere.
The call is nothing fancy:
std::sort(_detectedPhrases.begin(), _detectedPhrases.end(), PhraseSmartPtrParseCreationComparer());
Is there something I'm missing or I should be looking elsewhere?