#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Simple program causes double free or corruption (!prev)


    Hey everyone,

    So I have a really simple C program that just serperates 1 file into 3 files based on category.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main() {
      char line[80];
      FILE *in  = fopen("bermuda.gps", "r");
      FILE *out1 = fopen("ufos.gps", "w");
      FILE *out2 = fopen("disappearances.gps", "w");
      FILE *out3 = fopen("others.gps", "w");
      while ( fscanf( in, "%79[\n]\n", line  ) ) { 
        if( strstr( line, "UFO" ) ) { 
          fprintf( out1, "%s\n", line );
        } else if( strstr( line, "Dispapperances" ) ) { 
          fprintf( out2, "%s\n", line );
        } else {
          fprintf( out3, "%s\n", line );
        }   
      }
      fclose( out1 );
      fclose( out1 );
      fclose( out1 );
      return 0;
    }
    It compiles without errors or warnings.

    When I run it I get

    *** glibc detected *** ./bermuda-pluss: double free or corruption (!prev): 0x0000000001eec250 ***

    It creates all of the files but does not write anything to them before crashing.

    Any one know whats going on?

    Thank you.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Here is the bermuda.gps file

    30.685163,-68.137207,Type=Yeti
    28.304380,-74.575195,Type=UFO
    29.132971,-71.136475,Type=Ship
    28.343065,-62.753906,Type=Elvis
    27.868217,-68.005371,Type=Goatsucker
    30.496017,-73.333740,Type=Disappearance
    26.224447,-71.477051,Type=UFO
    29.401320,-66.027832,Type=Ship
    37.879536,-69.477539,Type=Elvis
    22.705256,-68.192139,Type=Elvis
    27.166695,-87.484131,Type=Elvis
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Code:
      fclose( out1 );
      fclose( out1 );
      fclose( out1 );
    You're closing the same file THREE times.

    > 30.496017,-73.333740,Type=Disappearance
    This isn't how you spelled it in the code.
    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

IMN logo majestic logo threadwatch logo seochat tools logo