On Apr 30, 2012 6:20 PM, "Da Beave" <dab...@gmail.com> wrote:
> int main(int argc, char **argv)
> {
> struct json_object *new_obj;
> new_obj = json_tokener_parse("{\"cat\":[\"666\"],\"rtss\":null,\"rtcc\":null,\"rep\":1}\"");
> printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
>
> new_obj = json_object_object_get(new_obj, "cat");
Right here you lost the reference to your original object. You need another struct json_object variable so you can work with the things you pull out of the one you got from json_tokener_parse().
Eric
Hi, Eric.I don't know the following function whether is right or
not.My english is poor,sorry.
{
struct json_object *obj_1;
struct json_object *obj_2;
obj_1 = json_tokener_parse("{\"cat\":[\"666\"],\"rtss\":null,
\"rtcc\":null,\"rep\": 1}\"");
obj_2 = json_object_object_get(obj_1, "cat");
....
json_object_put(obj_1);
ison_object_put(obj_2);
return 0;
}
My question is whether I need decrease the reference
obj_1(json_object_put(obj_1)) before return the function, and obj_2?
If I use json_object_put(obj_1) in a sub function,I get a error,"***
glibc detected *** double free or corruption (!prev)".
On Jun 16, 2012 9:31 PM, "vistamin" <feiyan...@gmail.com> wrote:
>
> Hi,Eric.Thank you for your quick reply.
> I upload a attachment and show how I use json-c lib in my project, not only parsing a configuration file(json format) but also encoding/decoding message in a private protocol.
> The error "***glibc detected *** double free or corruption (!prev)" puzzle me a long time, So I want to know whether I made a mistake when I use some functions in the json-lib.
>
Well, since you commented out the extra json_object_put() calls it looks like it should work. I would recommend trimming down you code to a smaller test case to see if you can narrow down when the error happens.
Eric
On Jun 18, 2012 5:58 AM, "vistamin" <feiyan...@gmail.com> wrote:
>
> Sorry ,this is no memory leak when I use json_object_put(obj_1) and not json_object_put(obj_2),I made a statistic mistake.
> But the "double free ..."error still appear.
If you're running this on linux I would recommend using valgrind to try to track down where the problem is coming from. If you pass it the --track-origins=yes option it should tell you not only where the second, invalid free call happened but also where the memory was first freed.
That should help you determine whether you're doing something wrong, such as calling json_object_put too many times, or whether json-c has a bug.
If it's a bug in json-c and you can put together a *small* test case, I will take a look at fixing it.
Eric