Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json_object_object_del() and Segmentation fault #430

Closed
santosh0705 opened this issue Jul 9, 2018 · 6 comments
Closed

json_object_object_del() and Segmentation fault #430

santosh0705 opened this issue Jul 9, 2018 · 6 comments

Comments

@santosh0705
Copy link

I searched for similar issue but didn't get any so posting my issue here. The below code is resulting Segmentation fault. The line which causing has been marked. If I comment the line there is no errors. Can't figure out why it is not working. I want to release all the memory used by json-c once I read the values from json.

Code:

    struct json_object *obj = json_tokener_parse(addr); // <- Reading JSON from file
...
...
    struct json_object *v_obj, *p_obj;
    if (json_object_object_get_ex(obj, "listen", &v_obj)) {
        if (json_object_object_get_ex(v_obj, "port", &p_obj))
            port = json_object_get_int(p_obj);
        if (json_object_object_get_ex(v_obj, "ip", &p_obj))
            ip = json_object_get_string(p_obj);
    }
    json_object_object_del(p_obj, ""); // <- This cause Segmentation fault
    json_object_object_del(v_obj, "");
    json_object_object_del(obj, "");

Json:

{
  "listen": {
    "port": 3000,
    "ip": "127.0.0.1"
  },
...
...
}
@ploxiln
Copy link
Contributor

ploxiln commented Jul 9, 2018

If you want to free the entire obj, just json_object_put(obj), and remove all your calls to json_object_object_del(). json_object_object_get_ex() does not increase the reference count on the children, so just freeing the root will free the whole tree.

It does not make sense to call json_object_object_del() with an empty string for the object member key to be deleted.

@hawicz
Copy link
Member

hawicz commented Jul 10, 2018

What @ploxiln said, but also: if you're want to keep using the string after freeing the object, you need to copy it, perhaps with strdup.

http://json-c.github.io/json-c/json-c-0.13.1/doc/html/json__object_8h.html#a9ee29ca8d79896e15007131527f6002e
"... The returned string memory is managed by the json_object and will be freed when the reference count of the json_object drops to zero."

@hawicz hawicz closed this as completed Jul 10, 2018
@santosh0705
Copy link
Author

Got it. Thank you so much 😄

@santosh0705
Copy link
Author

json_object_put(jobj) was working with older version (v0.11-4) of json-c from CentOS 7 base repository.

But with the latest version of json-c compiled from source I'm getting below error:

json_object.c:189: json_object_put: Assertion `jso->_ref_count > 0' failed.

@santosh0705
Copy link
Author

Still I'm not getting how json_object_put() works 😞

I've two json objects in my program:

    struct json_object *conf = json_object_new_object();
    struct json_object *jobj = NULL;
...
...
    jobj = json_tokener_parse_ex(tok, buf, st.st_size);
...
...
    json_object_put(conf);
    if (jobj != NULL)
        json_object_put(jobj);

If i delete any of the two json_object_put() than I'm not getting any errors.
Someone please help me to correctly use json_object_put().

@santosh0705
Copy link
Author

Got it working. Few sub-objects of conf were referencing sub-objects in jobj due to which it was happening.
Used json_object_deep_copy() to copy the sub-objects from jobj and added in conf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants