Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved][QJsonDocument] Is QJsonDocument a thread safe?
Forum Updated to NodeBB v4.3 + New Features

[Solved][QJsonDocument] Is QJsonDocument a thread safe?

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 2 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    love8879201
    wrote on last edited by love8879201
    #1

    i am using QJsonDocument::toJson(QJsonDocument::Compact),
    but it will get wrong json format when multi-thread enviroment,
    my jsonUtil method was below and only used local variable, what wrong about QJsonDcoument?
    Does someone can help me with some hint?

    const char* JsonUtils::toJson(const QJsonObject &obj) {
        QJsonDocument document(obj);
        QByteArray byteArray = document.toJson(QJsonDocument::Compact);
        char *json = byteArray.data();
        return json;
    }
    
    const char* JsonUtils::toJson(const QJsonArray &array) {
        QJsonDocument document(array);
        QByteArray byteArray = document.toJson(QJsonDocument::Compact);
        char *json = byteArray.data();
        return json;
    }
    
    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      Your problem is not related to threads.

      Both your functions return invalid pointers. Your QByteArray is a local variable, so it is destroyed when the function returns. That means your char* pointer points to memory that has been deleted.

      Your functions should return QByteArray instead.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      L 1 Reply Last reply
      0
      • JKSHJ JKSH

        Hi,

        Your problem is not related to threads.

        Both your functions return invalid pointers. Your QByteArray is a local variable, so it is destroyed when the function returns. That means your char* pointer points to memory that has been deleted.

        Your functions should return QByteArray instead.

        L Offline
        L Offline
        love8879201
        wrote on last edited by
        #3

        @JKSH

        wow, this was a big mistake, too stupid to see that

        thanks for your reply, i would try return object instead.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved