Reputation: 132
I saw some random behavior while scanning my code through Valgrind. i have freed all possible memory blocks still I seeing Valgrind say 1 block is not properly freed.
Objective: Creating the 3 level json by using cJSON.c then in the second block modified the json format.
#include<stdio.h>
#include<stdlib.h>
#include"cJSON.h"
int main () {
cJSON *root = NULL, *root1 = NULL , *arrays = NULL, *array = NULL;
root = cJSON_CreateObject();
root1 = cJSON_CreateObject();
arrays = cJSON_CreateArray();
cJSON_AddItemToObject(root,"check",root1);
cJSON_AddItemToObject(root1, "innercheck",cJSON_CreateString("just to check"));
cJSON_AddItemToObject(root1, "array", arrays);
cJSON_AddItemToArray(arrays, array = cJSON_CreateObject());
cJSON_AddItemToObject(array, "innnerarray", cJSON_CreateNumber(1));
char *out = NULL;
printf("===================================================\n");
out = cJSON_Print(root);
printf("%s\n", out);
/* further operation now going to extract the value from json and readd new key */
cJSON *json_param = NULL, *exe1 = NULL , *exe2 = NULL;
json_param = cJSON_Parse(out);
exe1 = cJSON_GetObjectItem(json_param , "check");
cJSON_DeleteItemFromObject(exe1, "check");
exe2 = cJSON_CreateObject();
cJSON_AddItemToObject(exe2, "test", exe1);
char *out2 = NULL;
out2 = cJSON_Print(exe2);
printf("====================================================\n");
printf("%s\n", out2);
if(root) {
cJSON_Delete(root);
root = NULL;
}
if(json_param) {
cJSON_Delete(json_param);
json_param = NULL;
}
if(out) {
free(out);
out = NULL;
}
if(out2) {
free(out2);
out2 = NULL;
}
return 0;
}
Valgrind scan report :
==23708== Memcheck, a memory error detector
==23708== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==23708== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==23708== Command: ./object
==23708==
===================================================
{
"check": {
"innercheck": "just to check",
"array": [{
"innnerarray": 1
}]
}
}
====================================================
{
"test": {
"innercheck": "just to check",
"array": [{
"innnerarray": 1
}]
}
}
==23708==
==23708== HEAP SUMMARY:
==23708== in use at exit: 64 bytes in 1 blocks
==23708==
> total heap usage: 29 allocs, 28 frees, 2,661 bytes allocated
==23708==
==23708== 64 bytes in 1 blocks are definitely lost in loss record 1 of 1
==23708== at 0x4C2FDFB: malloc (vg_replace_malloc.c:309)
==23708== by 0x108F47: cJSON_New_Item (cJSON.c:214)
==23708== by 0x10C9F8: cJSON_CreateObject (cJSON.c:2410)
==23708==
> by 0x108BFA: main (object.c:35)
==23708==
==23708== LEAK SUMMARY:
==23708== definitely lost: 64 bytes in 1 blocks
==23708== indirectly lost: 0 bytes in 0 blocks
==23708== possibly lost: 0 bytes in 0 blocks
==23708== still reachable: 0 bytes in 0 blocks
==23708== suppressed: 0 bytes in 0 blocks
==23708==
==23708== For lists of detected and suppressed errors, rerun with: -s
==23708== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
I have freed all the possible memory block but unable to get why still I am getting 1 block is not properly free.
Upvotes: 2
Views: 492
Reputation: 3012
It seems there are two problems in your code.
Firstly, you need to release the exe2
object (which you had allocated using cJSON_CreateObject()
) before exiting.
cJSON_Delete(exe2);
Secondly ... You need to detach the check
object from json_param
before adding it to the exe2
object. Otherwise you will have a double free when you call cJSON_Delete(exe2)
- the library will attempt to free the same object twice (since the same object will have been attached as a child to more than one object).
So change
exe1 = cJSON_GetObjectItem(json_param, "check");
cJSON_DeleteItemFromObject(exe1, "check");
to simply
exe1 = cJSON_DetachItemFromObject(json_param, "check");
Note that I removed the line cJSON_DeleteItemFromObject(exe1, "check");
- as it is redundant as far as I can tell (unless you meant to remove the innercheck
element instead).
Upvotes: 3