title: SIGABRT while attempting to free a linked list
body: <p>I was working on some older texts our professor gave us to prepare for the upcoming exam, and I ran into this problem.</p>
  
  <p>My task is to read information from a text file which is structured as follows:</p>
  
  <p>[decimal number],[roman number (string)],[o or u (optimized or unoptimized roman number)]</p>
  
  <p>for a few thousand lines and store that information in a binary search tree, using the decimal number as key. Each branch must also contain the number of occurences of that number and a list of the various roman versions encountered, with the optimized one at the top of the list.
  Then free everything.</p>
  
  <p>My code (in c):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct list //list node
  {
    char *rom;
    struct list *next;
  };
  
  struct branch //main tree branch
  {
    int count, dec;
    struct list *list;
    struct branch *right, *left;
  };
  
  struct list *insertnode(struct list *head, char *rom, char opt) //creates a head or creates and adds a new node to the end
  {
    struct list *new;
    if(!head) //if !head, make one
      {
        new = malloc(sizeof(new));
        new-&gt;rom = malloc(sizeof(char)*(strlen(rom)+1));
        strcpy(new-&gt;rom, rom);
        new-&gt;next = NULL;
        return new;
      }
    if(opt == 'o') //if the roman form is optimized, put it in front of all the others
      {
        new = malloc(sizeof(new));
        new-&gt;rom = malloc(sizeof(char)*(strlen(rom)+1));
        strcpy(new-&gt;rom, rom);
        new-&gt;next = head;
        return new;
      }
    head-&gt;next = insertnode(head-&gt;next, rom, opt); //recursive insertions
    return head;
  }
  
  struct branch *insertbranch(struct branch *root, int dec, char *rom, char opt) //creates a root or creates and adds a new branch
  {
    struct branch *new;
    if(!root) //if !root, make a root...
      {
        new = malloc(sizeof(new));
        new-&gt;list = insertnode(new-&gt;list, rom, opt);
        new-&gt;dec = dec;
        new-&gt;count = 1;
        new-&gt;right=new-&gt;left=NULL;
        return new;
      }
    if(dec&lt;root-&gt;dec) root-&gt;left = insertbranch(root-&gt;left, dec, rom, opt); //branch on the left, recursive
    else if(dec&gt;root-&gt;dec) root-&gt;right = insertbranch(root-&gt;right, dec, rom, opt); //branch on the right, recursive
    else //if there already is such a branch, increase its count
      {
        root-&gt;count += 1;
        root-&gt;list = insertnode(root-&gt;list, rom, opt);
      }
    return root;
  }
  
  void freelist(struct list *head) //frees list 'head'
  {
    struct list *tmp;
    while(head)
      {
        tmp = head;
        head = head-&gt;next;
        free(tmp-&gt;rom);
        free(tmp); // &lt;- OFFENDING LINE
      }
    return;
  }
  
  void freetree(struct branch *root) //frees tree 'root'
  {
    if(!root) return;
    freetree(root-&gt;left); //recursive!
    freetree(root-&gt;right); //and on the right
    free(root-&gt;right);
    free(root-&gt;left);
    freelist(root-&gt;list);
    free(root);
    return;
  }
  
  int main()
  {
    struct branch *root;
    struct list *list;
    int dec, i=1, n;
    char rom[30], opt;
    FILE *file = fopen("rom.csv", "r");
    if(!file) //is the file even there?
      return 1;
  
    while(fscanf(file, "%d,%[^,],%c\n", &amp;dec, rom, &amp;opt)==3) //go through the file and fill tree
      root = insertbranch(root, dec, rom, opt);
  
    freetree(root);
    printf("Goodbye!\n");
    return 0;
  }
  </code></pre>
  
  <p>After debugging I isolated the problem: in function "freelist", when it reaches the command "free(tmp)" the program aborts. I have no idea what the cause could be. I even check to make sure that the node head exists.</p>
  
  <p>Thanks for any help!</p>