Debugging Memory Corruption

This is a discussion on Debugging Memory Corruption within the C Programming forums, part of the General Programming Boards category; Good evening all. I am currently having problems with memory corruption in one of my programs this results in the ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    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:

    Code:
    *** glibc detected *** malloc(): memory corruption: 0x0804ec90 ***
    ./buildandrun.sh: 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:

    Code:
    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/libc.so.6
    #5  0xb7dd2fb9 in abort () from /lib/tls/i686/cmov/libc.so.6
    #6  0xb7e06d3a in __fsetlocking () from /lib/tls/i686/cmov/libc.so.6
    #7  0xb7e0f106 in free () from /lib/tls/i686/cmov/libc.so.6
    #8  0xb7e1092f in malloc () from /lib/tls/i686/cmov/libc.so.6
    #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.

    Code:
    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;
    	list->count++;
    	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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Could you show the code where you are using DLLAddObject function?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    > 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
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    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
    Posts
    2
    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, 12:57 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Memory Corruption
    By cyreon in forum C Programming
    Replies: 3
    Last Post: 11-15-2007, 02: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, 01:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21