cjson · Section 3
cjson_destroy
NAME
cjson_destroy - free a DOM tree and NULL the handle
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cjson.h> cjson_destroy(node);
DESCRIPTION
Frees node and every descendant reachable from it, then sets node to NULL. When node is already NULL, nothing is freed. Since the macro assigns NULL to node after the destroy, node has to be an assignable lvalue of type cjson and cannot be an arbitrary expression. Application code must use this form, because that assignment is what turns an accidental second destroy of the same variable into a no-op instead of a double free. Each node is freed through the allocator that created it, the one the caller gave to the cjson_create_*_mp() or cjson_parse_mp() call that made the node, so the macro takes no allocator argument. The subtree is torn down without recursing once per level, so a document of any depth is destroyed without exhausting the call stack. That holds for a document that the caller builds directly with cjson_list_push() and cjson_dictionary_set() too, although those calls have no depth bound at all.
PARAMETERS
node Assignable cjson variable that names the root of the subtree
to free. It is NULL on return, and a NULL variable is a no-op.
It must name a tree the caller owns, which means one that is
not attached to a parent container.
RETURN VALUE
None. The macro expands to a statement, so it has no value.
NOTES
The macro evaluates node exactly once. node must be a modifiable lvalue such as a variable or an array element, and an expression with a side effect such as handles[i++] destroys and clears one element.
WARNINGS
A node that cjson_dictionary_get(), cjson_list_get() or cjson_get() returns is a borrowed reference into a tree, and the parent owns it, not the caller. Destroying it frees the node while the parent points at it, and the later destroy of the parent is a double free. Destroy only a root that the caller owns. To remove a child, call cjson_dictionary_remove() or cjson_list_remove(), which free what they remove themselves. A node that the caller passes to cjson_list_push() or cjson_dictionary_set() belongs to the container from that moment. On most failure paths the call has deep-freed it, so destroying the node again is a double free there as well. Only ccol_invalid_args leaves the node with the caller, untouched, and these are the cases that give it: - A container that is NULL or of the wrong kind. - A node that is attached to a container. - A node that is the container itself. - An attach that would make a cycle. - For cjson_dictionary_set(), a key that is NULL or not valid UTF-8. Every other failure, ccol_not_enough_memory included, takes the node and frees it. cjson_list_push(3) gives the exact split of ownership.
EXAMPLES
cjson doc = cjson_parse("{\"k\":[1,2,3]}", NULL); if (!doc) return; /* ... use doc ... */ cjson_destroy(doc); /* doc is NULL afterwards */ cjson_destroy(doc); /* harmless no-op */
SEE ALSO
cjson_declare_scoped(3), cjson_parse(3), cjson_clone(3), cjson_remove(3), cjson_list_push(3)