Thread: Malloc hanging

  1. #1
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20

    Malloc hanging

    I've got a strange C problem that I've never seen before and can't figure out why its happening.

    I'm writing a GTK app and I keep a linked list of data which is graphically represented as a CList. There are two ways in which items can be added; after the GUI loads it reads a configuration file building the list as it goes, or by adding new items via the GUI.

    When reading the configuration file there are a set of functions to allocate a new item and populate its members, then calls a function to insert it into the linked list. when a new item is added via the GUI there is a function to read the data from the GUI, allocate the item and call the function to insert it.

    The problem is that when reading configuration malloc hangs when allocate the new item.

    If there is only 1 item the config file things work fine, and adding items from the GUi works perfectly on any number of adds/delets.

    here is one of the functions that hang on malloc. This is called from the configuration loading function in another file.
    Code:
    struct os_list_item *
    os_list_new_other (char *new_title, char *new_unknown)
    {
    	struct os_list_item *new_item = malloc (sizeof (struct os_list_item));
    	if (new_item == NULL) {
    		return NULL;
    	}
    	new_item->title = new_title;
    	new_item->type = OTHER;
    	new_item->def = (os_list_size == -1 ? TRUE : FALSE);
    	new_item->data = NULL;
    	new_item->unknown = new_unknown;
    	new_item->next = NULL;
    	new_item->previous = NULL;
    
    	os_list_put(new_item);
    	return new_item;
    }
    The return value is only compared to NULL (never stored in a local variable)

    Also, when it hangs the CPU usage skyrockets.

    I appologize for the long post, but I wanted to provide as much info as possible. If you need information, the CVS can be viewed here: http://sourceforge.net/cvs/?group_id=17229

    Any help would be appreciated, and if you need more information I will be happy to provide

    - Joseph Monti

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Sounds a bit strange

    Are you sure its malloc() hanging? I mean, have you managed to step through it with a debugger, or at least do some printf()'s just before and just after the malloc() call.

    Can you replicate this problem in a smaller application, something that we could compile and run?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    Yeah, i'm sure its malloc. I've put printf's before and after the malloc function and it hangs before printing the second printf, it is able to complete the malloc on the first insert, but successive calls are unsuccessful.

    I have also stepped through with a debugger and execution hangs on that line. I've been using Anjuta for development which has a nice frontend to gdb.

    I would be surprised if I could generate the error in a small example, but I will surely try when I can get back to my PC on monday (I'm away for the weekend).

    The project CVS can be anonymously accessed with these commands:
    Code:
    cvs -d:pserver:[email protected]:/cvsroot/grubconf login 
    cvs -z3 -d:pserver:[email protected]:/cvsroot/grubconf co grubconf
    or the WebCVS can be viewed here:
    http://cvs.sourceforge.net/cgi-bin/v....cgi/grubconf/

    To get the problem to occur there is a line that needs to be commented and a sample configuration in the execution directory. I will be more than happy to provide instructions if someone is so inclined.

    I've used malloc() many times before and never seen anything like this. Very strange! :(

    Thank for any help!
    - Joe
    Last edited by Joe Monti; 02-02-2003 at 12:50 AM.
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I really doubt it is malloc that's hanging. Are you flushing your streams when using printf to debug? More than likely your "os_list_put" function is at fault, since one works and not any additional ones.

    I suspect (haven't looked at your CVS tree) that you've got a bad insertion loop. This would explain the cpu usage.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    Yeah, I've tried flushing the stream after the printf()'s.

    When I stepped through with a debugger the malloc() line is where it hangs.

    I've inspected the os_list_put() function and it looks good. There is also no loops that it does for any insertion (it inserts at front of the linked list). The os_list_put() is used by the working allocation functions and they work well. And that function does work more than once successfully when items are added in the GUI, and not at configuration loading.

    I have a lot of experience with C, and this is the strangest thing I've seen.

    There is a small urgency to this problem. This bug is holding back the initial release of the software (called GrubConf: http://joe.tgpr.org/Grubconf under the GNU GPL) which was scheduled for Feb 1st.

    Thank you for your help!
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  6. #6
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    I don't know whether malloc is at fault or some obscure problem in your code, but if you continue to have problems it might be worth writing a simple arena allocator for your project, or maybe maintaining a free list of these nodes and dealing with all such allocations elsewhere.
    .sect signature

  7. #7
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    EUREKA!!!!

    I found the problem. Malloc can get confused when malloced memory has buffer overflow. I sifted through all my mallocing and found a stupid error. Instead of malloc (strlen (my_string) + 1) I said malloc (strlen (my_string + 1)) thus shortening the allocated length instead of lengthening.
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Congratulations

    Good find... and err yeah, simple mistake

    Thanks for posting your findings too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 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. malloc always setting length of 8?
    By Zarkhalar in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2004, 11:36 PM