'QT qDeleteAll and new afterwards same memory address and crash
I have a weird behavior when using qDeleteAll on a QMap<QString, Record*>. First I delete my values in the QMap with qDeleteAll and then I use m_data.clear() to empty the QMap.
void ClassName::function1(QMap<QString, QString> filter)
{
qDebug() << "function1 called";
QString query = "postgrestAdress?";
if (filter.size()){
for (QMap<QString, QString>::iterator i = filter.begin(); i != filter.end(); i++ ) {
query.append("&").append(i.key()).append("=").append(i.value());
}
}
postgrestLambdaFucntion(query,[this, query](QList<QMap<QString,QString>> result)
{
qDeleteAll(m_data);
m_data.clear();
m_users.clear();
for(auto &line : result)
{
Record *record = Mapper::mapQMapToRecord(line);
qDebug() << "created Record:" << record << " with id:" << record->id;
m_data.insert(line.value("material_number")+ "_" +QString::number(record->id), record);
if (!m_users.contains(line.value("user"))){
m_users.append(line.value("user"));
}
}
nextFunction(0);
});
}
Consider this as a record:
class Record : public QObject
{
Q_OBJECT
public:
explicit Record(QObject *parent = nullptr) : QObject(parent){};
~Record() {
qDebug() << "record " << this << "with id" << this->id << " is destroyed";
};
int id = 0;
QString material_number = "";
QString user = "";
}
When I call function1 with an already populated m_data list I get this debug information:
>Returing table from postgrest...
>record Record(0x564813686040) with id 1 is destroyed
>record Record(0x5648136f74b0) with id 33 is destroyed
>record Record(0x56481376a6b0) with id 15 is destroyed
>record Record(0x564813708c00) with id 43 is destroyed
>record Record(0x564813c28090) with id 44 is destroyed
>record Record(0x564813768eb0) with id 2 is destroyed
>record Record(0x5648136eb7a0) with id 14 is destroyed
>created Record: Record(0x564813768eb0) with id: 15
>created Record: Record(0x564813c28090) with id: 2
>created Record: Record(0x564813708c00) with id: 43
>created Record: Record(0x56481376a6b0) with id: 33
>created Record: Record(0x5648136f74b0) with id: 14
>created Record: Record(0x564813686040) with id: 1
>created Record: Record(0x564813de45b0) with id: 44
Inside the Mapper-Function I create new Record-Classes
Record* Mapper::mapQMapToRecord(QMap<QString, QString> line)
{
Record* record = new Record(nullptr);
record->id = line.value("id").toInt();
record->user = line.value("user");
return record;
}
Note that the Memory-Address of those new created objects are the same as the ones qDeleteAll removed. In the next function I make more requests and add data to the records, which are now "not available" and I get nullptr exceptions.
If I don't use "qDeleteAll" it works. I can also use a for-loop and use "deleteLater" from QObject without errors. I just want to know this behaviour better or why it happens. (it happens in debug and release builds)
Anybody has insights for me?
Best Regards
exa.byte
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
