Thread: malloc causing sigabrt with memwatch inclusion

  1. #1
    Registered User
    Join Date
    Mar 2011
    Hey folks. This one is really stumping me. Part of the project requirements for assignments in my C class is "No memory leaks." So I finished my project, and then went in and included memwatch.h in all my c files and all that fun stuff. Now a perfectly working program (to my knowledge) is encountering a memory corruption at a specific call to malloc, which I isolated with gdb.

    First let me show you the involved type definitions:
    typedef struct lbbe * Lbbe;
    struct lbbe
      char *name;
      char *number;
      int stars;
    typedef struct bstnode *BSTNode;
    struct bstnode
      Lbbe entry;
      BSTNode left;
      BSTNode right;
    Then the various code segments leading up the the call to malloc

    from main.c
    BSTNode read_file(BSTNode root, char* filename, int (*compare)(void*,void*))
      FILE *file;
      if((file = fopen(filename, "r")) == NULL)
        printf("Failed to open file\n");
      char name[30];
      char number[15];
      int stars;
      char line[150];
      Lbbe entry = NULL;
      while(fgets(line, 150, file) != NULL)
        sscanf(line,"%s %s %d", name, number, &stars);
        entry = lbbe_construct(strdup(name), strdup(number), stars);
        root = bst_insert(root, entry, compare); //follow this rabbit hole
        printf("entry inserted\n");
      return root;
    Then the call to insert
    BSTNode bst_insert(BSTNode current, Lbbe x, int (*compare)(void*,void*))
      printf("Inserting node \n");
      if(current == NULL)
        current = bstnode_create(x); //further down the rabbit hole
        if(bstnode_compare(current, x->name, compare) <= 0)
          current->left = bst_insert(current->left, x, compare);
          current->right = bst_insert(current->right, x, compare);
      return current;
    and finally to the constructor thats causing the issue
    BSTNode bstnode_create(Lbbe l)
      printf("attempting to malloc space for a node %d \n",sizeof(BSTNode));
      BSTNode node = NULL;
      node = malloc(sizeof(BSTNode)); //Why is this causing SIGABRT?!
      printf("malloc'd space for node \n");
      node->entry = l;
      node->right = NULL;
      node->left = NULL;
      return node;
    The crash is occurring at that commented line. My professor recommended commenting out any calls to free. That didn't do anything. He also suggested allocating more space than necessary, which also didnt do anything. I'm confused as to why this error is occurring at this specific malloc, whereas other calls to malloc, such as the lbbe_construct in the read_file function, work fine.

    Thanks for any help!

    here is the error output:
    *** glibc detected *** bt: malloc(): memory corruption: 0x080513d0 ***
    ======= Backtrace: =========
    ======= Memory map: ========
    08048000-08050000 r-xp 00000000 00:1f 325435797  /home/afwheele/cs302/asn3/bt
    08050000-08051000 rw-p 00008000 00:1f 325435797  /home/afwheele/cs302/asn3/bt
    08051000-08072000 rw-p 08051000 00:00 0          [heap]
    b7d00000-b7d21000 rw-p b7d00000 00:00 0 
    b7d21000-b7e00000 ---p b7d21000 00:00 0 
    b7e52000-b7e53000 rw-p b7e52000 00:00 0 
    b7e53000-b7f9c000 r-xp 00000000 08:03 572220     /lib/tls/i686/cmov/
    b7f9c000-b7f9d000 r--p 00149000 08:03 572220     /lib/tls/i686/cmov/
    b7f9d000-b7f9f000 rw-p 0014a000 08:03 572220     /lib/tls/i686/cmov/
    b7f9f000-b7fa2000 rw-p b7f9f000 00:00 0 
    b7fad000-b7fb7000 r-xp 00000000 08:03 554901     /lib/
    b7fb7000-b7fb8000 rw-p 0000a000 08:03 554901     /lib/
    b7fb8000-b7fbf000 rw-p b7fb8000 00:00 0 
    b7fbf000-b7fc0000 r-xp b7fbf000 00:00 0          [vdso]
    b7fc0000-b7fda000 r-xp 00000000 08:03 554988     /lib/
    b7fda000-b7fdc000 rw-p 00019000 08:03 554988     /lib/
    bfd98000-bfdad000 rw-p bffeb000 00:00 0          [stack]
    and the intricacies from gdb
    (gdb) where
    #0  0xb7f50410 in __kernel_vsyscall ()
    #1  0xb7e0f085 in raise () from /lib/tls/i686/cmov/
    #2  0xb7e10a01 in abort () from /lib/tls/i686/cmov/
    #3  0xb7e47b7c in ?? () from /lib/tls/i686/cmov/
    #4  0xb7e50356 in ?? () from /lib/tls/i686/cmov/
    #5  0xb7e51cad in malloc () from /lib/tls/i686/cmov/
    #6  0x08049ba3 in mwMalloc (size=4, file=0x804fdb3 "bstnode.c", line=18) at memwatch.c:893
    #7  0x0804d8b5 in bstnode_create (l=0x80513c4) at bstnode.c:18
    #8  0x0804d9af in bst_insert (current=0x0, x=0x80513c4, compare=0x804d803 <lbbe_compare>) at bst.c:19
    #9  0x0804df8d in read_file (root=0x0, filename=0xbfa0b59c "testfile", compare=0x804d803 <lbbe_compare>)
        at main.c:27
    Last edited by drshmoo; 03-12-2011 at 09:54 AM.

  2. #2
    Registered User
    Join Date
    Apr 2008
    You're not allocating the right amount of memory. Since BSTNode is defined as "struct bstnode *", sizeof(BSTNode) gives you the size of the pointer (probably 4 bytes) instead of the size of what it points to. As a result, when you write to the struct you're writing past the bounds of what was allocated.

    You were just lucky that it didn't crash before. When you do something like this, it basically sets off a time bomb which can trigger a crash at some completely unrelated area of your program. You should also check your other mallocs, specifically in lbbe_construct where I suspect you may be doing something similar.

    I haven't fooled around with memwatch, but I'm guessing it's smart enough to realize this isn't proper and it throwing the error on the malloc to inform you of this.

  3. #3
    Registered User
    Join Date
    Mar 2011
    So you're suggesting I replace it with "struct bstnode" instead of BSTNode?


    Yep! Worked. Thank you! Looking back at old projects I did exactly what you suggested. Looks like I just overlooked this one. Thank you so much!
    Last edited by drshmoo; 03-12-2011 at 10:06 AM. Reason: adding comment

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SIGABRT on simple malloc
    By KVH in forum C Programming
    Replies: 4
    Last Post: 11-26-2010, 04:54 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM
  5. Header inclusion causing errors
    By cjschw in forum C++ Programming
    Replies: 12
    Last Post: 08-11-2004, 03:48 PM