Memory Leak Using JSON-C
json_tokener_parse()
will create an object which must be deleted.
in this case
json_object_put(new_obj);
is required.
NO, We need to call json_object_put only once for root object as long as we are not explicitly allocating memory to json-object and this worked for me.....!!
Yes, I believe your code will leak memory. The problem is that you are overwriting your new_obj pointer multiple times. Your code should be something like this:
struct json_object *new_obj, *fuu_obj, *foo_obj;
new_obj = json_tokener_parse(strRawJSON);
fuu_obj = json_object_object_get(new_obj, "FUU");
if(NULL == new_obj){
SYS_OUT("\nFUU not found in JSON");
return NO;
}
foo_obj = json_object_object_get(new_obj, "FOO");
if(NULL == new_obj){
SYS_OUT("\nFOO not found in JSON");
return NO;
}
json_object_put(foo_obj);
json_object_put(fuu_obj);
json_object_put(new_obj);
Please let me know if this works for you. If you want more help, json-c has a reference count mode which can give you more information about objects. Let me know and I can elaborate on this more.