0

I've the following JSON object:

{
  "heartbeat": {
    "status": 0,
    "tempID": 1210254506,
    "firmware": "obu-hmi-commsignia",
    "sync": 1,
    "upgrade": 0,
    "log": 0,
    "fix": 3,
    "rsu": 1,
    "numTx": 5813,
    "numRx": 5685,
    "numRemotes": 0,
    "numTIMs": 0,
    "numIntersections": 0,
    "txPower": -52
  }
}

My code snippet is the following: I should call the function json_object_put(). The problem is that I can call this function for each json element (commented part of the code) except for the hb element (json_object_put(hb)). I obtain this error:

json_object.c:189: json_object_put: Assertion `jso->_ref_count > 0' failed.

This is the code:

    while (1)
    {
        rsu_array = malloc(1024 * sizeof(int));
        rsu_array_size = 0;
        obu_array = malloc(1024 * sizeof(int));
        obu_array_size = 0;
        sem_post(&sem);
        total_sent_messages = 100;
        total_received_messages = 100;
        sleep(1);
        sem_wait(&sem);
    
        json_object *hb = json_object_new_object();
        json_object *temp_jobj = json_object_new_object();
        json_object *temp_int1 = json_object_new_int(1);
        json_object *temp_int0 = json_object_new_int(0);
        json_object *tid = json_object_new_int(1210254506);
        json_object *fix = json_object_new_int(3);
        json_object *temp_string = json_object_new_string("...");
        json_object_object_add(temp_jobj, "status", temp_int0);
        json_object_object_add(temp_jobj, "tempID", tid);
        json_object_object_add(temp_jobj, "firmware", temp_string);
        json_object_object_add(temp_jobj, "sync", temp_int1);
        json_object_object_add(temp_jobj, "upgrade", temp_int0);
        json_object_object_add(temp_jobj, "log", temp_int0);
        json_object_object_add(temp_jobj, "fix", fix);
        json_object *jrsu = json_object_new_int(rsu_array_size);
        json_object_object_add(temp_jobj, "rsu", jrsu);
        json_object *jTX = json_object_new_int(total_sent_messages);
        json_object_object_add(temp_jobj, "numTx", jTX);
        json_object *jRX = json_object_new_int(total_received_messages);
        json_object_object_add(temp_jobj, "numRx", jRX);
        json_object *jobu = json_object_new_int(obu_array_size);
        json_object_object_add(temp_jobj, "numRemotes", jobu);
        json_object *jnumTIM = json_object_new_int(100);
        json_object_object_add(temp_jobj, "numTIMs", jnumTIM);
        json_object *jnumINTERSECTIONS = json_object_new_int(100);
        json_object_object_add(temp_jobj, "numIntersections", jnumINTERSECTIONS);
        json_object *txPOWER = json_object_new_int(100);
        json_object_object_add(temp_jobj, "txPower", txPOWER);
        json_object_object_add(hb, "heartbeat", temp_jobj);
    
        char heartbeat_str[2048] = "";
        strcpy(heartbeat_str, json_object_to_json_string(hb));
 
    
    
        /*json_object_put(txPOWER);
        json_object_put(jnumINTERSECTIONS);
        json_object_put(jnumTIM);
        json_object_put(jobu);
        json_object_put(jRX);
        json_object_put(jTX);
        json_object_put(jrsu);
        json_object_put(temp_string);
        json_object_put(fix);
        json_object_put(tid);
        json_object_put(temp_int0);
        json_object_put(temp_int1);
        
        json_object_put(temp_jobj);*/
        json_object_put(hb);
    
        free(rsu_array);
        free(obu_array);
    }
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
ofstack773
  • 49
  • 5
  • For a [MCVE](https://stackoverflow.com/help/mcve) you could remove most of the fields and just keep a few. – Gerhardh Jun 14 '22 at 09:16
  • *I can call this function for each json element* I think you mustn't call `json_object_put` on any object that was added to another JSON object unless you explicitely request a reference. – Gerhardh Jun 14 '22 at 09:19
  • Sure, it was only to specify that I obtain the error only for the hb element – ofstack773 Jun 14 '22 at 09:20
  • You have some issue with reference counting. For debuggig I suggest to comment out every instruction between calls to `json_object_new_object` and `json_object_put` and see if you still get that error. Then step by step add them again. As far as I understand, counter should be 1 initially and releasing the object should work. You might have memory corruption. – Gerhardh Jun 14 '22 at 13:23

1 Answers1

0

In this unit test from jason-c repository, json_object is declared outside of the loop and json_object_put is also used outside of the loop. So maybe you need to try the same approach

json_object *hb = json_object_new_object();

while (1)
{
    rsu_array = malloc(1024 * sizeof(int));
    rsu_array_size = 0;
    obu_array = malloc(1024 * sizeof(int));
    obu_array_size = 0;
    sem_post(&sem);
    total_sent_messages = 100;
    total_received_messages = 100;
    sleep(1);
    sem_wait(&sem);

    json_object *temp_jobj = json_object_new_object();
    json_object *temp_int1 = json_object_new_int(1);
    json_object *temp_int0 = json_object_new_int(0);
    json_object *tid = json_object_new_int(1210254506);
    json_object *fix = json_object_new_int(3);
    json_object *temp_string = json_object_new_string("...");
    json_object_object_add(temp_jobj, "status", temp_int0);
    json_object_object_add(temp_jobj, "tempID", tid);
    json_object_object_add(temp_jobj, "firmware", temp_string);
    json_object_object_add(temp_jobj, "sync", temp_int1);
    json_object_object_add(temp_jobj, "upgrade", temp_int0);
    json_object_object_add(temp_jobj, "log", temp_int0);
    json_object_object_add(temp_jobj, "fix", fix);
    json_object *jrsu = json_object_new_int(rsu_array_size);
    json_object_object_add(temp_jobj, "rsu", jrsu);
    json_object *jTX = json_object_new_int(total_sent_messages);
    json_object_object_add(temp_jobj, "numTx", jTX);
    json_object *jRX = json_object_new_int(total_received_messages);
    json_object_object_add(temp_jobj, "numRx", jRX);
    json_object *jobu = json_object_new_int(obu_array_size);
    json_object_object_add(temp_jobj, "numRemotes", jobu);
    json_object *jnumTIM = json_object_new_int(100);
    json_object_object_add(temp_jobj, "numTIMs", jnumTIM);
    json_object *jnumINTERSECTIONS = json_object_new_int(100);
    json_object_object_add(temp_jobj, "numIntersections", jnumINTERSECTIONS);
    json_object *txPOWER = json_object_new_int(100);
    json_object_object_add(temp_jobj, "txPower", txPOWER);
    json_object_object_add(hb, "heartbeat", temp_jobj);

    char heartbeat_str[2048] = "";
    strcpy(heartbeat_str, json_object_to_json_string(hb));



    /*json_object_put(txPOWER);
    json_object_put(jnumINTERSECTIONS);
    json_object_put(jnumTIM);
    json_object_put(jobu);
    json_object_put(jRX);
    json_object_put(jTX);
    json_object_put(jrsu);
    json_object_put(temp_string);
    json_object_put(fix);
    json_object_put(tid);
    json_object_put(temp_int0);
    json_object_put(temp_int1);
    
    json_object_put(temp_jobj);*/

    free(rsu_array);
    free(obu_array);
}

json_object_put(hb);
  • I would need every time to free the object and create a new one, so I need to call the create and the put inside the loop – ofstack773 Jun 14 '22 at 09:14
  • In your JSON example heartbeat is the **top-level parent object**, why do you need to create a **top-level parent object** every time you create a child? when you can just add children to it. that's why I'm suggesting taking it out of the loop. – David Kviloria Jun 14 '22 at 09:22
  • I tried with your solution, but unfortunately the error is the same... – ofstack773 Jun 14 '22 at 09:30