I get a couple different crashes running the below code, sometimes a segmentation fault and sometimes ASSERT: "&other != this" in file C:/Qt/5.12.3/mingw73_64/include/QtCore/qstring.h, line 958
.
The code to reproduce this is below.
#include <QList>
#include <QDebug>
class Tag
{
public:
Tag(const QString& name)
{
this->name = new QString(name);
}
Tag(const Tag& tag)
{
this->name = new QString(tag.getName());
}
~Tag()
{
if (name != nullptr) delete name;
}
QString getName() const
{
return *name;
}
static bool sortByName(const Tag& t1, const Tag& t2)
{
return t1.getName() < t2.getName();
}
private:
QString* name;
};
int main(int argc, char **argv)
{
QList<Tag> tags;
Tag a("a");
Tag b("b");
Tag c("c");
Tag d("d");
Tag e("e");
tags << e << c << a << b << d;
qDebug() << "before sorting";
for (Tag tag : tags) {
qDebug() << tag.getName();
}
std::sort(tags.begin(), tags.end(), Tag::sortByName);
qDebug() << "after sorting";
for (Tag tag : tags) {
qDebug() << tag.getName();
}
return 0;
}
I have noticed that if the initial QList
is filled with pointers to Tag
objects and sortByName()
is changed to accept pointers instead, it does not cause issues. I'm at a loss as to why.