Skip to content

Free memory of custom dom in jsonlint.c #18

@miloyip

Description

@miloyip

I found that the jsonlint sample did not free the memory for the DOM tree. The following is my implementation. This may be needed to show proper use of the API in real application.

static void tree_free(json_val_t* v) {
    switch (v->type) {
    case JSON_OBJECT_BEGIN:
        for (int i = 0; i < v->length; i++) {
            tree_free(v->u.object[i]->val);
            free(v->u.object[i]->key);
            free(v->u.object[i]);
        }
        free(v->u.object);
        break;

    case JSON_ARRAY_BEGIN:
        for (int i = 0; i < v->length; i++)
            tree_free(v->u.array[i]);
        free(v->u.array);
        break;

    default:
        free(v->u.data);
        break;
    }
    free(v);
}

int main(int argc, char **argv)
{
    // ...
        if (use_tree) {
            json_val_t *root_structure;
            ret = do_tree(&config, argv[i], &root_structure);
            if (ret)
                exit(ret);
            if (!verify)
                print_tree(root_structure, output);
            tree_free(root_structure);  // <--
        }
    // ...
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions