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

Free memory of custom dom in jsonlint.c #18

Open
miloyip opened this issue Aug 22, 2014 · 1 comment
Open

Free memory of custom dom in jsonlint.c #18

miloyip opened this issue Aug 22, 2014 · 1 comment

Comments

@miloyip
Copy link

miloyip commented Aug 22, 2014

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);  // <--
        }
    // ...
} 
@vincenthz
Copy link
Owner

@miloyip Thanks, that's probably useful to show API usage indeed. Could you put the patch in a pull request ?

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

2 participants