[Asterisk-Dev] Properly Destroying Variables

Matthew Boehm mboehm at cytelcom.com
Thu Sep 15 09:17:03 MST 2005


Consider the following code:

int my_func(char *val) {
   struct ast_variable *var, *tmp;

   var = ast_load_realtime("family", "name", val, NULL);
   if(var) {
     tmp = var;
     while(tmp) {
       if(!strcasecmp(tmp->name, "bleh"))
         ast_verbose(VERBOSE_PREFIX_3 "Value is %s\n", tmp->value);
       tmp = tmp->next;
     }
   }
   ast_variables_destroy(var);
   return 1;
}

Where/How does tmp get freed/destroyed properly?

Another question, is it safe to do this (I say no since you loose the 
pointer to the first element of var):

int my_func(char *val) {
   struct ast_variable *var;

   var = ast_load_realtime("family", "name", val, NULL);
   if(var) {
     while(var) {
       if(!strcasecmp(var->name, "bleh"))
         ast_verbose(VERBOSE_PREFIX_3 "Value is %s\n", var->value);
       var = var->next;
     }
   }
   ast_variables_destroy(var);
   return 1;
}

I guess the real question is, what is the proper way to loop thru an 
ast_variable struct and still be able to properly destroy it? Must you 
use the 2 struct method or can it be done with 1?

-Matthew (aka drmac on mantis)




More information about the asterisk-dev mailing list