Can't free my json object in C (assertion failed)

407 Views Asked by At

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);
    }
1

There are 1 best solutions below

3
On

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);