Json-C has this clumsy and poorly-documented reference counting which is giving us problems. In particular, we have an object containing children, and want to replace a particular child with
json_object_object_add(parent, "child name", new_child)
.
Now we know that this transfers ownership of new_child
, that's no problem. But what about the old child? We could manually remove it with json_object_object_del
, which doesn't delete the old child (but leaks it). So it appears the following solution is a proper replacement:
json_object *manual = json_object_object_get(parent, "child name");
json_object_object_del(parent, "child name");
json_object_put(manual);
json_object_object_add(parent, "child name", new_child);
However, we're wondering if json_object_object_add
is smart enough to make the first three steps redundant. This would be a better design because we'd prefer an atomic replacement - if the new child cannot be added for whatever reason, we should keep the old child.