Thread: freeing global buffer

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Arrow freeing global buffer

    I have one of these situations that makes me think I don't really understand something I thought I understood, which it's been true before...I have a global buffer that I want to free() and reuse like this:

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    char *buffer;
    
    void testfunc (int toggle) {
    	char X[]="XXXXXXXXX";
    	if (toggle==0) { buffer=malloc(11);
    		strcpy(buffer,X);}
    	else {buffer=realloc(buffer,strlen(buffer)+11);
    		strcat(buffer,X);
    	}
    }
    
    int main() {
    	testfunc(0);
    	testfunc(1);
    	puts(buffer);
    	free(buffer);            // end of 1st iteration
    	testfunc(0);
    	testfunc(1);
    	puts(buffer);
    	free(buffer);
    	return 0;
    }
    This code works, but does anyone know what could cause it to produce a double-free/corruption abort at "end of 1st iteration" if:
    • THERE IS ONLY ONE free() call previously in the script,
    • "buffer" was just malloc'd and realloc'd as above, previous to the free(),
    • the total number of bytes allocated to buffer is >0

    To me this just does not make sense, it's something I've done many times before (freeing and reusing a malloc'd buffer in a loop) and again, it does not make sense to me, but it keeps happening anyway: I have a global char*, I allocate it memory in a function, it serves it's purpose, then I free it for reuse in main() -- and I get a double free/corruption abort. What factors am I ignorant of?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you tried using valgrind to identify the problem.

    To me it looks ok, but I haven't got a good test-suite here at home to try it out properly.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Strange.

    edit: tested it and runs nicely. Check again (maybe accindentally mixing execution files and running something else?)
    Last edited by C_ntua; 11-26-2008 at 02:08 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    Have you tried using valgrind to identify the problem.

    Mats
    No, I'll but perhaps today is the day to try something new, like software...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    odd if you are really getting that behaviour. Some sort of race condition? Working with this in gcc 4.0.3 on my Ubuntu platform I seem to be ok. Using valgrind would be a good start. Out of curiosity is puts() working as expected as this does have a return variable and happens to be the last call before the free().

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    using gdb would be a another place to look within and running through your code.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by slingerland3g View Post
    odd if you are really getting that behaviour. Some sort of race condition? Working with this in gcc 4.0.3 on my Ubuntu platform I seem to be ok. Using valgrind would be a good start. Out of curiosity is puts() working as expected as this does have a return variable and happens to be the last call before the free().
    Unfortunately I am not having the same problem anymore (after changing the mechanism in the function for another reason) so I may never be able to figure out why (the change was from while recv>0 to while recv>=0, which made some difference to content of buffer, but in either case it was malloc'd and did have some content). w/r/t to puts, yes, it was working which means I should be able to free() the variable!?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print out a buffer
    By SwarfEye in forum C Programming
    Replies: 4
    Last Post: 09-08-2006, 09:32 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. defining and using a global class
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2004, 09:51 PM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM