Search code examples
qtqvariantqjson

QVariantMap crash in destructor


I am building a JSON-object with Qt and convert it to a QString using QJson. This (normally) works fine and it does in this case but in the destructor of my Qt data structure, it crashes with an Access Violation. The Object is built fine, it is sent over my network connection and after the function ends, my application crashes.

My code looks like this:

void bar()
{
    QVariantMap data;
    data.insert("Id", 1);
    QList<QVariant> list; //QVariantList

    for (QMap<...>:ConstIterator ... ) //Loop through a Map
    {
        QMap<QString, QVariant> singleEntry; //QVariantMap
        singleEntry.insert("LocalId", it.value());
        QList<QVariant> entryList; //QVariantList
        for (...) //Loop through another structure
        {
            entryList.append("foo");
        }
        singleEntry.insert("List", entryList);
        list.append(singleEntry);
    }
    data.insert("Entries", list);

    QJson::Serializer.serialize(data); // Works fine
} // Crash here

If I remove the inner loop, which builds up entryList, everything works fine. It seems that the destructor of data cannot delete the contents but I have no idea, why. The whole data structure seems to be fine while serializing it (and I hope that QJson does not change anything in the given data) but it cannot be cleaned up..

Best Regards, Tobias


Solution

  • I got a little workaround, which fits my needs. I have still no idea, why this crash happens, but I know, which should be the problem.

    I tried to build up a static structure like this:

    QVariantMap
      QVariantList
        QVariantMap
          QVariantList
    

    and it crashes. If I remove the QVariantList at the bottom and add QVariantMap or anything else instead, it is working fine. I think this is a problem with the nesting level in this case.

    I have now just joined my list as a comma-seperated QString and then it works fine.

    If anyone of you has an idea, why the crash in destructing such a nested struct (another information: doesnt matter if a allocate the QVariants in heap and delete them myself or stack) and how to fix it, please let me know.