Thread: Debugging Memory Corruption

  1. #1
    Registered User
    Join Date
    May 2007

    Debugging Memory Corruption

    Good evening all. I am currently having problems with memory corruption in one of my programs this results in the following error:

    *** glibc detected *** malloc(): memory corruption: 0x0804ec90 ***
    ./ line 3: 15773 Aborted                 ./build
    It is proving very difficult to fix and I have yet to find a solution. It only happens with a specific set of apparently harmless data (an object which holds "foobar\n") and only after another string before comprising of "!!!!!!!" is created. This seems to only happen when I attempt to insert these values into my Doubly Linked List. What limited I know about gdb does not seem to be assisting myself much. The output is below:

    Program received signal SIGABRT, Aborted.
    0xb7f17410 in ?? ()
    (gdb) bt
    #0  0xb7f17410 in ?? ()
    #1  0xbf877d10 in ?? ()
    #2  0x00000006 in ?? ()
    #3  0x00003dc5 in ?? ()
    #4  0xb7dd1811 in raise () from /lib/tls/i686/cmov/
    #5  0xb7dd2fb9 in abort () from /lib/tls/i686/cmov/
    #6  0xb7e06d3a in __fsetlocking () from /lib/tls/i686/cmov/
    #7  0xb7e0f106 in free () from /lib/tls/i686/cmov/
    #8  0xb7e1092f in malloc () from /lib/tls/i686/cmov/
    #9  0xb7ee21fe in DLLNCreate (previous=0x0, next=0x0, index=0, data=0x804a190) at DoublyLinkedList.c:7
    #10 0xb7ee22e9 in DLLAddObject (list=0x804ccd8, object=0x804a190) at DoublyLinkedList.c:38
    #11 0x08048afd in main () at main.m:28
    Contrary to the above I can find no fault in DLLNCreate (below) or DDLAddObject.

    struct DLLNode {
    	struct DLLNode * previous;
    	struct DLLNode * next;
    	unsigned index;
    	void * data;
    struct DLList {
    	struct DLLNode * first;
    	struct DLLNode * last;
    	unsigned count;
    DLLNode *
    DLLNCreate(DLLNode * previous, DLLNode * next, unsigned index, void * data)
    	DLLNode * node = (DLLNode *)malloc(sizeof(DLLNode));
    	node->previous = previous;
    	node->next = next;
    	node->index = index;
    	node->data = data;
    	return node;
    DLList *
    DLLAddObject(DLList * list, void * object)
    	DLLNode * last = list->last;
    	DLLNode * newNode = DLLNCreate(last,NULL,last==NULL ? 0 : (last->index+1),object);
    	if (last != NULL) {
    		last->next = newNode;
    	if ((list->first) == NULL) {
    		list->first = newNode;
    	list->last = newNode;
    	return list;
    My question is this, does anyone know of any tutorials or other guides which I can follow to troubleshoot this? I would like to learn how to fix it on my own.

    I can provide any information and any of the source required to resolve this issue.

    Thank you.
    Last edited by Maskawanian; 05-23-2007 at 09:17 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    Could you show the code where you are using DLLAddObject function?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    Ė David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > node->previous = previous;
    > node->next = next;
    So where do you do
    previous->next = node;
    next->previous = node;
    to complete the insertion of a node into the list?

    These of course need to be guarded for NULL in the cases of inserting a node into an empty list, at the head of the list or at the tail of the list.

    Personally, I would move all the next/prev updating back into DLLAddObject(), and make
    DLLNode *
    DLLNCreate(unsigned index, void * data)
    	DLLNode * node = malloc(sizeof(*node));
    	node->previous = NULL;
    	node->next = NULL;
    	node->index = index;
    	node->data = data;
    	return node;
    and put all the next/prev smarts into whatever function calls it.

    Do you correctly initialise your DLList variable to NULL, NULL, 0 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    When malloc() or free() crash, it is usually because of one of three things:

    1. You have overflowed a dynamic buffer somewhere. Look at all code which sticks data into dynamically allocated memory, especially code in loops.

    2. You have manipulated an object after calling free() on it.

    3. You have dereferenced a completely bogus pointer which just coincidentally happens to point into the heap, thereby corrupting the heap.

    EDIT: Furthermore, looking for the bug in the code shown in the stack trace is barking up the wrong tree. Heap corruption usually happens far from the place the crash occurs. Chances are, the call stack has NO useful information in it.

  5. #5
    Registered User
    Join Date
    May 2007
    Thank you for all your suggestions everyone, I want you all to know I haven't abandoned this thread, but I will give a status when and if I resolve it .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Memory Corruption
    By cyreon in forum C Programming
    Replies: 3
    Last Post: 11-15-2007, 03:52 AM
  4. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 03:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM