I have seen multiple documentation citing that use of cJSON_Print like the below example needs to be accompanied with a call for free(), else it will cause a memory leak.
char* char_json = cJSON_Print(jsonvar)
Will I get a memory leak issue if I use it like this for printing or logging?
fprintf(stderr, "Value of JSON is %s", cJSON_Print(jsonvar));
Is there a chance of a memory leak here? Also, is this a good practice? Using one single char ptr to assign and store multiple JSON Print outputs.
char* one_var_for_all_json;
one_var_for_all_json = cJSON_Print(jsonvar_one);
//use the value in char array
one_var_for_all_json = cJSON_Print(jsonvar_two);
//use the new value
//finally free it with
free(one_var_for_all_json);
The answers in this Should the return value of cJSON_Print() be freed by the caller? only answer scenarios where the output of cJSON is allocated to a variable in that scope. I have doubts when it's output is directly passed to something like a printf statement. Also my second doubt isn't answered in the accused duplicate.