json-c parsing issue.

795 views
Skip to first unread message

Da Beave

unread,
Apr 30, 2012, 7:20:05 PM4/30/12
to jso...@googlegroups.com
I have the following type of json input coming from a webserver:

{ "cat": [ "666" ], "rtss": null, "rtcc": null, "rep": 1 }

I'm attempting to parse it,  like thus: 

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");
  printf("Cat = %s\n", json_object_to_json_string(new_obj));

  new_obj = json_object_object_get(new_obj, "rep");
  printf("rep = %s\n", json_object_to_json_string(new_obj));
  json_object_put(new_obj);
  return 0;
}

However, when I run the test program,  I can only get one value.   Here's the example output:

new_obj.to_string()={ "cat": [ "666" ], "rtss": null, "rtcc": null, "rep": 1 }
Cat = [ "666" ]
rep = null

Note the "rep = null" value. I would expect that to be "1".  I know I must be missing something.  It seems that I can only pull one value at a time,  but not two.  Thanks in advance from a new json-c user :)





Da Beave

unread,
Apr 30, 2012, 9:27:38 PM4/30/12
to jso...@googlegroups.com
This seemed to have done the trick:

   cat  = json_object_get_string(json_object_object_get(new_obj, "cat"));
   rep  = json_object_get_string(json_object_object_get(new_obj, "rep"));
   rtss  = json_object_get_string(json_object_object_get(new_obj, "rtss"));
   rtcc  = json_object_get_string(json_object_object_get(new_obj, "rtcc"));

Eric Haszlakiewicz

unread,
Apr 30, 2012, 9:37:10 PM4/30/12
to jso...@googlegroups.com

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

Da Beave

unread,
May 1, 2012, 10:41:25 AM5/1/12
to jso...@googlegroups.com
Thanks Eric..  I sorta had one of those "oh doh!" *facepalm* moments.  However,  I appreciate the response.  

vistamin

unread,
Jun 16, 2012, 2:54:41 AM6/16/12
to json-c
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 5月1日, 上午9时37分, Eric Haszlakiewicz <haw...@gmail.com> wrote:
> On Apr 30, 2012 6:20 PM, "Da Beave" <dabe...@gmail.com> wrote:> int main(int argc, char **argv)

Eric Haszlakiewicz

unread,
Jun 16, 2012, 11:06:12 AM6/16/12
to jso...@googlegroups.com
On Sat, Jun 16, 2012 at 1:54 AM, vistamin <feiyan...@gmail.com> wrote:
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)".


You don't need to call json_object_put on obj_2 because json_object_object_get() doesn't increment the reference count.  That means that because obj_2 is still part of obj_1 it will be automatically freed when obj_1 is freed by the json_object_put(obj_1) call.

If you want to work with obj_2 separately (e.g. perhaps you want to have a function that returns just that object, and not the surrounding obj_1), you would need to explicitly increase the reference count with json_object_get(obj_2)  and *only* in that case would you need to call json_object_put(obj_2).

eric

vistamin

unread,
Jun 16, 2012, 10:31:12 PM6/16/12
to jso...@googlegroups.com
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.


在 2012年6月16日星期六UTC+8下午11时06分12秒,Eric写道:
code_message.c

Eric Haszlakiewicz

unread,
Jun 16, 2012, 11:45:55 PM6/16/12
to jso...@googlegroups.com


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

vistamin

unread,
Jun 17, 2012, 6:23:41 AM6/17/12
to jso...@googlegroups.com
Thanks a million again.

在 2012年6月17日星期日UTC+8上午11时45分55秒,Eric写道:

vistamin

unread,
Jun 18, 2012, 2:38:48 AM6/18/12
to jso...@googlegroups.com
Hi,Eric. Trouble you again,^_^.

I test the memory usage when I use json_object_put function on obj_1 or obj_2. 
Because I use json_token_parse() function, json_object_object_get() and json_object_put() functions in a sub function, So I can check memory usage easily.
By the way, I use "top" command to see the memory usage.
  1. If I use json_object_put(obj_1) and comment json_object_put(obj_2),there is a memory leak.The memory usage changes from 11% to 24%.When the progarm exit, there is a error "*** glibc detected *** double free or corruption (!prev)"
  2. If I use json_object_put(obj_2) and comment json_object_put(obj_1),there is a memory leak.The memory usage changes from 11% to 27%.When the progarm exit, there is a error "*** glibc detected *** double free or corruption (!prev)"
  3. If I use json_object_put(obj_1) and use json_object_put(obj_2), the memory usage changes from 11% to 13%.When the progarm exit, there is a error "*** glibc detected *** double free or corruption (!prev)"
  4. If I comment json_object_put(obj_1) and comment json_object_put(obj_2),there is a memory leak.The memory usage changes from 11% to 30%.The Error doesn't appear.How strange!
I don't konw why.

在 2012年6月16日星期六UTC+8下午11时06分12秒,Eric写道:

vistamin

unread,
Jun 18, 2012, 6:57:59 AM6/18/12
to jso...@googlegroups.com
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.

在 2012年6月18日星期一UTC+8下午2时38分48秒,vistamin写道:

Eric Haszlakiewicz

unread,
Jun 18, 2012, 12:21:44 PM6/18/12
to jso...@googlegroups.com


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

Reply all
Reply to author
Forward
0 new messages