json_object_put crash

927 views
Skip to first unread message

lmpc

unread,
Oct 11, 2012, 6:19:09 AM10/11/12
to jso...@googlegroups.com
Hi guys

I'm trying to figure out what is happening to a piece of json code i wrote. I cant see what is wrong with that, but sometimes it gives me segmentation fault on the last json_object_put with the next trace:

arraylist.c:54
json_object.c:617
json_object.c:136

my code (resumed) is doing the following, with Elements = ["1234"]



struct json_object *new_obj, *ResultArray, *ResultObj = NULL;
new_obj = json_tokener_parse(Elements);

UINT32 arraySize = json_object_array_length(new_obj);
      
        for(UINT32 j=0; j < arraySize; j++)
        {
            struct json_object *obj = json_object_array_get_idx(new_obj, j);
            ResultObj = json_object_new_object();
            json_object_object_add(ResultObj, "id", obj);
            //adding some other objects (arrays, strings etc...)
            json_object_array_add(ResultArray, ResultObj);
      }

strncpy(BufferOut, json_object_to_json_string(ResultArray), BUFSIZE);
json_object_put(ResultArray);
json_object_put(new_obj); //crash here


thanks

Eric Haszlakiewicz

unread,
Oct 11, 2012, 9:21:31 AM10/11/12
to jso...@googlegroups.com

On Oct 11, 2012 5:19 AM, "lmpc" <luismpc...@gmail.com> wrote:
>
> I'm trying to figure out what is happening to a piece of json code i wrote. I cant see what is wrong with that, but sometimes it gives me segmentation fault on the last json_object_put with the next trace:
>

> my code (resumed) is doing the following, with Elements = ["1234"]
>
> struct json_object *new_obj, *ResultArray, *ResultObj = NULL;

I don't see where you set ResultArray.  Are you actually assigning anything to it?

Eric

lmpc

unread,
Oct 11, 2012, 10:52:56 AM10/11/12
to jso...@googlegroups.com
Thank you.

Yes i am assigning a new array, i forgot to paste the following line, sorry:

ResultArray = json_object_new_array();

Eric Haszlakiewicz

unread,
Oct 14, 2012, 3:06:43 PM10/14/12
to jso...@googlegroups.com
On Thu, Oct 11, 2012 at 9:52 AM, lmpc <luismpc...@gmail.com> wrote:
> Yes i am assigning a new array, i forgot to paste the following line, sorry:
>
> ResultArray = json_object_new_array();

Well, as far as I can tell, the code is working fine. I tried it with
both json-c 0.10, and the current development version, and it runs ok,
at least on the NetBSD system that I am using.
You haven't said which version of json-c you're using, nor have you
mentioned what OS you're on, or any other details of your environment.

Also, your so-called "stack trace" doesn't make any sense. The line
numbers you list don't match up with any code that could end up in the
files you list. e.g. in json-c 0.10, line json_object.c:617 is just
the start of a function definition, not actual code:
struct json_object* json_object_new_array(void)

In the current development version, it is this line inside of
json_object_new_string_len():
jso->_delete = &json_object_string_delete;
which clearly doesn't call anything in arraylist.c

If your simple test case only "sometimes" results in a segmentation
fault, then I'm inclines to think that something else is going on
(e.g. bad hardware).

I'd love to help you, but you'll need to provide more details.

Eric

-----

$ cat Makefile
all:
gcc -I ${JSON_C_HOME}/include/json/ -std=c99 test1.c
-L${JSON_C_HOME}/lib -ljson -Wl,-R${JSON_C_HOME}/lib

$ cat test1.c
#include "json.h"
#include "json_object.h"
#include <sys/types.h>
#include <string.h>
#include <stdio.h>

#define UINT32 uint32_t
#define BUFSIZE 16384

int main(int argc, char **argv)
{
const char *Elements = "[\"1234\"]";
char BufferOut[BUFSIZE];

struct json_object *new_obj, *ResultArray, *ResultObj = NULL;
new_obj = json_tokener_parse(Elements);
ResultArray = json_object_new_array();

UINT32 arraySize = json_object_array_length(new_obj);

for(UINT32 j=0; j < arraySize; j++)
{
struct json_object *obj = json_object_array_get_idx(new_obj, j);
ResultObj = json_object_new_object();
json_object_object_add(ResultObj, "id", obj);
//adding some other objects (arrays, strings etc...)
json_object_array_add(ResultArray, ResultObj);
}

strncpy(BufferOut, json_object_to_json_string(ResultArray), BUFSIZE);
json_object_put(ResultArray);
json_object_put(new_obj); //crash here

printf("%s\n", BufferOut);

return 0;
}

$ ./a.out
[ { "id": "1234" } ]

lmpc

unread,
Oct 24, 2012, 6:26:10 AM10/24/12
to jso...@googlegroups.com
Thanks Eric for your time,

I'm using a dedicated rtos and i had discover a bug on my memory manager module.
By now i'm not having any problems with json_object_put.

thanks
Reply all
Reply to author
Forward
0 new messages