I am using the Jansson library in my c++ code. In the library when any Jansson variable is created, the variable should be freed to release the reference and I am using the 'json_decref(json_t *json)' method to release. But sometimes it causes the double free problem and I didn't get the problem in runtime. Maybe it is not normal but I want to get a runtime error when I try to release some references. For example, I am running the following test code,
json_t *root = json_loads(any_json_message, 0, &error);
json_decref(root);
json_decref(root);
As you see there is a problem and I don't get any runtime error like double free.
Also, I am using CMake to compile my source code. Maybe any cmake flag fixes my question.
Any idea ?
The behaviour of accessing the pointed object is undefined once it has been freed. The documentation doesn't spell it out, but calling
json_decref
more than once (plus the times you've calledjson_incref
on the pointer) also has undefined behaviour.Well, Jansson doesn't give that for you.
Your compiler might provide a run time analyser that might be able to detect some undefined behaviour and terminate the program with a message. See the manual of your compiler for how to use the analyser.