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;
Then the various code segments leading up the the call to malloc
typedef struct bstnode *BSTNode;
Then the call to insert
BSTNode read_file(BSTNode root, char* filename, int (*compare)(void*,void*))
if((file = fopen(filename, "r")) == NULL)
printf("Failed to open file\n");
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
and finally to the constructor thats causing the issue
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);
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.
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;
Thanks for any help!
here is the error output:
and the intricacies from gdb
*** 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/libc-2.7.so
b7f9c000-b7f9d000 r--p 00149000 08:03 572220 /lib/tls/i686/cmov/libc-2.7.so
b7f9d000-b7f9f000 rw-p 0014a000 08:03 572220 /lib/tls/i686/cmov/libc-2.7.so
b7f9f000-b7fa2000 rw-p b7f9f000 00:00 0
b7fad000-b7fb7000 r-xp 00000000 08:03 554901 /lib/libgcc_s.so.1
b7fb7000-b7fb8000 rw-p 0000a000 08:03 554901 /lib/libgcc_s.so.1
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/ld-2.7.so
b7fda000-b7fdc000 rw-p 00019000 08:03 554988 /lib/ld-2.7.so
bfd98000-bfdad000 rw-p bffeb000 00:00 0 [stack]
#0 0xb7f50410 in __kernel_vsyscall ()
#1 0xb7e0f085 in raise () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7e10a01 in abort () from /lib/tls/i686/cmov/libc.so.6
#3 0xb7e47b7c in ?? () from /lib/tls/i686/cmov/libc.so.6
#4 0xb7e50356 in ?? () from /lib/tls/i686/cmov/libc.so.6
#5 0xb7e51cad in malloc () from /lib/tls/i686/cmov/libc.so.6
#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>)
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.
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!