1

i worked with json-c library in C language. I have the following code

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json/json.h>

int main()
{
    while(1)
    {
          testJson();
          Sleep(2000); //sleep 2 seconds
    }
    return 0;
}

void testJson()
{
      json_object *new_obj;
      char buf[] = "{ \"foo\": \"bar\", \"foo2\": \"bar2\", \"foo3\": \"bar3\" }"
      new_obj = json_tokener_parse(buf);
      json_object_object_add(new_obj, "d1", json_object_new_string(data1));
      json_object_object_add(new_obj, "d2", json_object_new_string(data2));
      .
      .
      .
      .

      printf("obj=%s",json_object_get_string(new_obj));
      json_object_put(new_obj);
}

It print json obj only once. After it produce Segmentation fault (core dumped) message. But it working perfectly when i modifying sleep time to 10 second instead of 2 second.

I need to create and delete json object within 2 second. is it possible with json-c library?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95

0 Answers0