Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
json-c parsing issue.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  12 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Da Beave  
View profile  
 More options Apr 30 2012, 7:20 pm
From: Da Beave <dabe...@gmail.com>
Date: Mon, 30 Apr 2012 16:20:05 -0700 (PDT)
Local: Mon, Apr 30 2012 7:20 pm
Subject: json-c parsing issue.

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 :)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Da Beave  
View profile  
 More options Apr 30 2012, 9:27 pm
From: Da Beave <dabe...@gmail.com>
Date: Mon, 30 Apr 2012 18:27:38 -0700 (PDT)
Local: Mon, Apr 30 2012 9:27 pm
Subject: Re: json-c parsing issue.

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"));


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Haszlakiewicz  
View profile  
 More options Apr 30 2012, 9:37 pm
From: Eric Haszlakiewicz <haw...@gmail.com>
Date: Mon, 30 Apr 2012 20:37:10 -0500
Local: Mon, Apr 30 2012 9:37 pm
Subject: Re: json-c parsing issue.

On Apr 30, 2012 6:20 PM, "Da Beave" <dabe...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Da Beave  
View profile   Translate to Translated (View Original)
 More options May 1 2012, 10:41 am
From: Da Beave <dabe...@gmail.com>
Date: Tue, 1 May 2012 07:41:25 -0700 (PDT)
Local: Tues, May 1 2012 10:41 am
Subject: Re: json-c parsing issue.

Thanks Eric..  I sorta had one of those "oh doh!" *facepalm* moments.
 However,  I appreciate the response.  


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
vistamin  
View profile  
 More options Jun 16 2012, 2:54 am
From: vistamin <feiyangena...@gmail.com>
Date: Fri, 15 Jun 2012 23:54:41 -0700 (PDT)
Local: Sat, Jun 16 2012 2:54 am
Subject: Re: json-c parsing issue.
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Haszlakiewicz  
View profile  
 More options Jun 16 2012, 11:06 am
From: Eric Haszlakiewicz <haw...@gmail.com>
Date: Sat, 16 Jun 2012 10:06:12 -0500
Local: Sat, Jun 16 2012 11:06 am
Subject: Re: json-c parsing issue.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
vistamin  
View profile  
 More options Jun 16 2012, 10:31 pm
From: vistamin <feiyangena...@gmail.com>
Date: Sat, 16 Jun 2012 19:31:12 -0700 (PDT)
Local: Sat, Jun 16 2012 10:31 pm
Subject: Re: json-c parsing issue.

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
4K Download

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Haszlakiewicz  
View profile  
 More options Jun 16 2012, 11:45 pm
From: Eric Haszlakiewicz <haw...@gmail.com>
Date: Sat, 16 Jun 2012 22:45:55 -0500
Local: Sat, Jun 16 2012 11:45 pm
Subject: Re: json-c parsing issue.

On Jun 16, 2012 9:31 PM, "vistamin" <feiyangena...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
vistamin  
View profile  
 More options Jun 17 2012, 6:23 am
From: vistamin <feiyangena...@gmail.com>
Date: Sun, 17 Jun 2012 03:23:41 -0700 (PDT)
Local: Sun, Jun 17 2012 6:23 am
Subject: Re: json-c parsing issue.

Thanks a million again.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
vistamin  
View profile  
 More options Jun 18 2012, 2:38 am
From: vistamin <feiyangena...@gmail.com>
Date: Sun, 17 Jun 2012 23:38:48 -0700 (PDT)
Local: Mon, Jun 18 2012 2:38 am
Subject: Re: json-c parsing issue.

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写道:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
vistamin  
View profile  
 More options Jun 18 2012, 6:57 am
From: vistamin <feiyangena...@gmail.com>
Date: Mon, 18 Jun 2012 03:57:59 -0700 (PDT)
Local: Mon, Jun 18 2012 6:57 am
Subject: Re: json-c parsing issue.

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写道:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Haszlakiewicz  
View profile  
 More options Jun 18 2012, 12:21 pm
From: Eric Haszlakiewicz <haw...@gmail.com>
Date: Mon, 18 Jun 2012 11:21:44 -0500
Local: Mon, Jun 18 2012 12:21 pm
Subject: Re: json-c parsing issue.

On Jun 18, 2012 5:58 AM, "vistamin" <feiyangena...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »