Skip to content

Function to free whole datatree #12

@lukasmacko

Description

@lukasmacko

I would like to suggest including a function that frees whole data tree including siblings. It doesn't make difference if there is only one node at root level, however if there are multiple nodes it would be handy. Currently, you can read data tree by one function call but you have to use loop to free it.

Example:

module example-module {
   namespace "urn:ietf:params:xml:ns:yang:example";
   prefix ie; 

   organization "organization";
   description  "example yang module";
   contact      "contact@example.com";

   container container {
      config true;
      list list {
         leaf leaf {
            type string;
         }
         leaf key1 {
            type string;
         }
         key "key1";
      }   
   }   
   leaf-list number{
      type uint16;
   }   
}
FILE *data = fopen("example-module.data","r"); 

struct lyd_node *data_tree = lyd_parse_fd(ctx, fileno(data), LYD_XML, 0); 
fclose(data); 
lyd_free(data_tree); /*This causes MEMLEAK if data contains container and leaflist*/

My suggestion:

/**
 * Free the subtree and its siblings.
 * @param [in] root
 */
void lyd_free_datatree(struct lyd_node *root){
    struct lyd_node *next = NULL;
    while(NULL != root){
        next = root->next;
        lyd_free(root);
        root = next;
    }
}

(The function name could be also lyd_free_with_siblings.)

What do you think? Or is there something I've overlooked?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions