From a7dcea9a6e6e5b4b2eccc2ed0d6803c8408ffd0e Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 3 Feb 2021 22:46:45 +0100 Subject: [bug] add mutex to critical region This fixes the segfault. mRootItem has to be deallocated to avoid memory leaks, but doing it concurrently with the dump of the json was a terrible idea. - Closes: #360 --- gui/qjsonmodel.cpp | 56 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'gui/qjsonmodel.cpp') diff --git a/gui/qjsonmodel.cpp b/gui/qjsonmodel.cpp index 4e36eb8..43260a0 100644 --- a/gui/qjsonmodel.cpp +++ b/gui/qjsonmodel.cpp @@ -23,13 +23,16 @@ * SOFTWARE. */ -#include +#include #include +#include #include #include "qjsonmodel.h" +std::mutex mtx; + QJsonTreeItem::QJsonTreeItem(QJsonTreeItem *parent) { mParent = parent; @@ -105,7 +108,6 @@ QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* paren if ( value.isObject()) { - //Get all QJsonValue childs for (QString key : value.toObject().keys()){ QJsonValue v = value.toObject().value(key); @@ -115,7 +117,6 @@ QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* paren rootItem->appendChild(child); } - } else if ( value.isArray()) @@ -123,7 +124,6 @@ QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* paren //Get all QJsonValue childs int index = 0; for (QJsonValue v : value.toArray()){ - QJsonTreeItem * child = load(v,rootItem); child->setKey(QString::number(index)); child->setType(v.type()); @@ -202,13 +202,16 @@ bool QJsonModel::load(QIODevice *device) bool QJsonModel::loadJson(const QByteArray &json) { + mtx.lock(); + auto const& jdoc = QJsonDocument::fromJson(json); + bool ok = false; + if (!jdoc.isNull()) { beginResetModel(); - - delete mRootItem; + delete mRootItem; if (jdoc.isArray()) { mRootItem = QJsonTreeItem::load(QJsonValue(jdoc.array())); @@ -219,43 +222,42 @@ bool QJsonModel::loadJson(const QByteArray &json) } endResetModel(); - // ??? emit dataChanged(QModelIndex(), QModelIndex(), {}); - return true; + ok = true; } - qDebug()<(index.internalPointer()); - switch (role) { case Roles::KeyRole: return item->key(); case Roles::ValueRole: return item->value(); - case Qt::DisplayRole: - { - if (index.column() == 0) - return QString("%1").arg(item->key()); - else if (index.column() == 1) - return QString("%1").arg(item->value()); - else - return QString(""); - + case Qt::DisplayRole: { + if (index.column() == 0) + return QString("%1").arg(item->key()); + else if (index.column() == 1) + return QString("%1").arg(item->value()); + else + return QString(""); } - case Qt::EditRole: - { + case Qt::EditRole: { if (index.column() == 1) return QString("%1").arg(item->value()); else @@ -366,8 +368,10 @@ Qt::ItemFlags QJsonModel::flags(const QModelIndex &index) const QJsonDocument QJsonModel::json() const { + mtx.lock(); auto v = genJson(mRootItem); + QJsonDocument doc; if (v.isObject()) { @@ -376,14 +380,16 @@ QJsonDocument QJsonModel::json() const doc = QJsonDocument(v.toArray()); } + mtx.unlock(); return doc; } -QJsonValue QJsonModel::genJson(QJsonTreeItem * item) const +QJsonValue QJsonModel::genJson(QJsonTreeItem * item) const { auto type = item->type(); int nchild = item->childCount(); + if (QJsonValue::Object == type) { QJsonObject jo; for (int i = 0; i < nchild; ++i) { @@ -391,7 +397,7 @@ QJsonValue QJsonModel::genJson(QJsonTreeItem * item) const auto key = ch->key(); jo.insert(key, genJson(ch)); } - return jo; + return jo; } else if (QJsonValue::Array == type) { QJsonArray arr; for (int i = 0; i < nchild; ++i) { -- cgit v1.2.3