cjson · Section 3
cjson_declare_scoped
NAME
cjson_declare_scoped - declare a cjson variable with automatic destruction
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/cjson.h> cjson_declare_scoped(var_name);
DESCRIPTION
Declares a cjson handle named var_name, sets it to NULL and attaches a cleanup attribute to it. When var_name goes out of scope, the cleanup frees the tree that the variable holds, exactly as cjson_destroy() does. It runs at the end of the enclosing block on every way out of it: A normal exit, break, continue, return and goto. Only store a tree that the caller owns in the variable, such as the result of cjson_parse() or of a cjson_create_*() function. A borrowed reference from cjson_get() must not go there, because the cleanup would free it while its parent holds it. The macro relies on a GCC and Clang extension.
PARAMETERS
var_name Name of the variable to declare.
EXAMPLES
{
cjson_declare_scoped(root);
root = cjson_parse("{\"k\":1}", NULL);
/* root is freed automatically at the closing brace */
}