Thread: *** glibc detected *** double free or corruption

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    *** glibc detected *** double free or corruption

    Why would I get the title error when executing this twice?

    Code:
    char t[30] = {0x0};
    char tt[16] = { 0 };
    time_t s;
    	
    		time(&s);
    	
    		sprintf(t,"%s",ctime(&s));
    		strncpy(tt, t + 11, 12);
    		tt[2] = '\0';
    		hours = atoi(tt);

  2. #2
    old man
    Join Date
    Dec 2005
    Posts
    90
    I couldn't get an error from the code provided, even executing it four times ...

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    It's part of a function...and it works perfectly the first time it goes through...but the second time it doesn't. Please help!! Any idea's at all.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post more code - there's nothing in that bit of code which would cause a problem.
    The real cause is usually elsewhere when it comes to malloc/free problems.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    Code:
    void Func_Get_Image_Details()
    	{
    		char a[MAX_DIR_LENGTH]={0x0};
    		char b[30] = {0x0};
    		char c[16] = {0x0};
    		int hours = 0;
    		int minutes = 0;
    		int seconds = 0;
    		FILE *in_two=popen("ls -l","r");
    		while(fgets(a,sizeof(a),in_two))
    		pclose(in_two);
    		
    		
    		FILE *in_five=popen("ls -w","r");
    		while(fgets(temp,sizeof(temp),in_five))
    		pclose(in_five);
    		
    		time_t s;
    	
    		time(&s);
    	
    		sprintf(b,"%s",ctime(&s));
    		strncpy(c, b + 11, 12);
    		c[2] = '\0';
    		hours = atoi(c);
    		
    		
    		
    	}

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while(fgets(a,sizeof(a),in_two))
    > pclose(in_two);
    So you read a line, close the stream, then try and read from it again?
    How does this make any sense?
    Use some braces to make your intentions explicit.

    Also, there is no 'temp' in scope to validate your 2nd loop.

    Also, what's with all the C99 code? That is all those mixed declarations and statements.

    > strncpy(c, b + 11, 12);
    Why copy 12 characters, when all you need is 2?
    If you're just looking to extract time components, look at gmtime() or localtime() to split a time_t into a struct tm.

  7. #7
    old man
    Join Date
    Dec 2005
    Posts
    90
    Something like this will work better

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* arbitray value for testing purposes; perhaps too small
     * ... it would likely be better to malloc() a nice-sized buffer */
    #define MAX_DIR_LENGTH 4096
    
    
    void
    test_popen (void)
    {
      char a[MAX_DIR_LENGTH]={0x0};
      char line[256];
      FILE *in_two;
    
      in_two = popen ("ls -l", "r");
      while (fgets (line, sizeof line, in_two))
        strcat (a, line);
    
      pclose (in_two);
      printf ("Directory contents: %s\n", a);
    }  
    
    int
    main (void)
    {
      test_popen();
      test_popen();
    
      return 0;
    }
    Including pclose() in your loop was obviously what caused your problem. Also, even if you kept the stream open, you were overwriting your buffer with every line.
    Last edited by eerok; 02-05-2006 at 01:38 PM.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    62
    The above now works, however, after about 23 or so loops I get a segmentation fault. Should I be using malloc or calloc...if so can anybody give me some pointers (new to c).

    Thanks..

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > however, after about 23 or so loops I get a segmentation fault
    Check the combined length of the output!.

    > in_two = popen ("ls -l", "r");
    You're doing this.

    Try this on the command line
    ls -l | wc -c

    What number does it print out - it's the number of characters of output that command generates.

    How does this compare with
    #define MAX_DIR_LENGTH 4096

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  3. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  4. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM