[Asterisk-Dev] Properly Destroying Variables

Michael Giagnocavo mgg-digium at atrevido.net
Thu Sep 15 09:45:15 MST 2005


>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?

tmp wasn't allocated by that code; it's just a temp reference to an item in
a list, so whatever destroys the list should take care of it if its supposed
to. The issue would be if ast_load_realtime allocates something that
ast_variables_destroy isn't cleaning up (if it's not being cleaned up some
other way).
 
>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;
>}

In this code, ast_variables_destroy is going to be passed null, so that
probably won't do what you want.

>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?

Not sure about realtime, but the other code I've seen is like your first
example. 
-Michael





More information about the asterisk-dev mailing list