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" } ]