Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

crash after QJsonArray toVariantList() ...



  • Hi,
    my application crashed with following implementation:

    template<class T> void Vector<T>::Read(const QJsonObject& io_json)
    {
        //Read values
        QJsonArray json_array = io_json[s_JSON_DATA_VALUE].toArray();
        QVariantList variant_list;
    
       //Qt Bug?
       variant_list = json_array.toVariantList();  //not working (implicit sharing / move semantics problem???)       
       //Qt Bug?
    }
    

    If i discard toVariantList() and use a for loop to copy all values from json_array to variant_list everything works fine. Please, could someone help me with that?

    Best regards,


  • Lifetime Qt Champion

    Hi,

    A bit more information is needed to test that. What do you put in io_json ? How do you generate it ?



  • Unfortunately it's a project of a colleague but as far as i know the OJsonObject passed to the function is retrieved from a file using QJsonDocument object() method.

    The file looks similar to this:

    {
        "min" : -100,
        "max" : 100,
        "val" : [
            45,2,
            34,9,
            17,6
        ]
    }

  • Lifetime Qt Champion

    Can you show how you are calling that function ?

    Can't reproduce your problem from your code and sample data.

    What version of Qt are you running ? On what OS ?



  • @ttuna said:

    variant_list = json_array.toVariantList();

    Try this:

    variant_list = json_array.toVariantList(QVariantList());
    

    if worked then your json is invalid



  • @Hamed-Masafi
    The variant_list is valid after creation. The problem occurs when the Read() function is going to be left.
    It seems that a stack object (i tip json_array) will be deleted and produces a crash.

    @SGaist
    Here are the lines which are calling the Read() function: (i have removed some validity checks)

    QFile load_file(load_path);
    QByteArray load_data = load_file.readAll();
    QJsonDocument load_doc(in_format == Json ? QJsonDocument::fromJson(load_data) : QJsonDocument::fromBinaryData(load_data));
    Read(load_doc.object());

  • Lifetime Qt Champion

    Can you create minimal compilable example that reproduces the crash ?


Log in to reply