Welcome to the Builder Academy

Question destroy_db() tearing down triggers struct

More
11 Feb 2025 06:22 #10538 by krell
In void destroy_db() in db.c, I found this code snippet that's meant to traverse the list of triggers and free them. In the code comments, there is talk of a memory leak. I don't know if this was meant to fix that.
Code:
  /* Triggers */   for (cnt=0; cnt < top_of_trigt; cnt++) {     if (trig_index[cnt]->proto) {       /* make sure to nuke the command list (memory leak) */       /* free_trigger() doesn't free the command list */       if (trig_index[cnt]->proto->cmdlist) {         struct cmdlist_element *i, *j;         i = trig_index[cnt]->proto->cmdlist;         while (i) {           j = i;           if (i->cmd)           free(i->cmd);         free(i);         i = j;                                                                    }       }       free_trigger(trig_index[cnt]->proto);     }     free(trig_index[cnt]);   }   free(trig_index); }

I changed it to the following. I think that it might work better. Unfortunately, I have to rebuild my main device, so I can't test it by building and running TBA on it. Thoughts?
Code:
/* Triggers */   for (cnt=0; cnt < top_of_trigt; cnt++) {     if (trig_index[cnt]->proto) {       /* make sure to nuke the command list (memory leak) */       /* free_trigger() doesn't free the command list */       if (trig_index[cnt]->proto->cmdlist) {         struct cmdlist_element *i, *j;         i = trig_index[cnt]->proto->cmdlist;         while (i) {           j = i->next;           if (i->cmd) {           free(i->cmd);           i->cmd = NULL;           }         free(j);         j = NULL;                                                                      }       }       free_trigger(trig_index[cnt]->proto);     }     free(trig_index[cnt]);     trig_index[cnt] = NULL;   }   free(trig_index);   trig_index = NULL; }

Please Log in or Create an account to join the conversation.

More
11 Feb 2025 21:27 - 11 Feb 2025 21:29 #10539 by thomas
This is an endless loop. i is never changed in the loop.
Code:
        while (i) {           j = i->next;           if (i->cmd) {            free(i->cmd);            i->cmd = NULL;           }           free(j);          j = NULL;                                                                      }
Last edit: 11 Feb 2025 21:29 by thomas.

Please Log in or Create an account to join the conversation.

Time to create page: 0.193 seconds