what does double free or corruption (!prev): mean? Thank u~

Discussion in 'C Programming' started by xiao, Aug 8, 2008.

  1. xiao

    xiao Guest

    Hey ~ everyone~
    What does this error mean?
    *** glibc detected *** double free or corruption (!prev):

    It occurs when i tried to write to a file like this :

    Write2DArrayInt(tcumulus, ncolumns,nrows, out);

    Write2DArrayInt is a function like this:

    void Write2DArrayInt(short int **Array, int Columns, int Rows, FILE
    *fp)
    {

    int i;

    for(i=0; i<Rows; i++){
    fwrite(Array, sizeof(short int),Columns, fp);
    }
    fclose(fp);
    }

    And tcumulus is defined as : short **tcumulus
     
    xiao, Aug 8, 2008
    #1
    1. Advertising

  2. xiao

    Guest

    Re: what does double free or corruption (!prev): mean? Thank u~

    On Aug 7, 6:22 pm, xiao <> wrote:
    > Hey ~ everyone~
    > What does this error mean?
    > *** glibc detected *** double free or corruption (!prev):


    Odds are that you've written past the end of an array allocated with
    malloc(). Or possibly called free() twice on the same pointer.

    You might try using something like ElectricFence to find the bug.

    > It occurs when i tried to write to a file like this :
    >
    > Write2DArrayInt(tcumulus, ncolumns,nrows, out);
    >
    > Write2DArrayInt is a function like this:
    >
    > void Write2DArrayInt(short int **Array, int Columns, int Rows, FILE
    > *fp)
    > {
    >
    > int i;
    >
    > for(i=0; i<Rows; i++){
    > fwrite(Array, sizeof(short int),Columns, fp);
    > }
    > fclose(fp);
    >
    > }
    >
    > And tcumulus is defined as : short **tcumulus


    Is it possible that this is being called on a file that's already
    closed? It's a little odd for a function to close a file which it
    didn't open, so there could be a bug lurking.

    Otherwise this looks fine, so the bug could be somewhere else in your
    code. It is common for malloc() errors to go undetected until much
    later in the program's execution.
     
    , Aug 8, 2008
    #2
    1. Advertising

  3. xiao

    xiao Guest

    Re: what does double free or corruption (!prev): mean? Thank u~

    On Aug 7, 8:56 pm, wrote:
    > On Aug 7, 6:22 pm, xiao <> wrote:
    >
    > > Hey ~ everyone~
    > > What does this error mean?
    > > *** glibc detected *** double free or corruption (!prev):

    >
    > Odds are that you've written past the end of an array allocated with
    > malloc(). Or possibly called free() twice on the same pointer.
    >
    > You might try using something like ElectricFence to find the bug.
    >
    >
    >
    > > It occurs when i tried to write to a file like this :

    >
    > > Write2DArrayInt(tcumulus, ncolumns,nrows, out);

    >
    > > Write2DArrayInt is a function like this:

    >
    > > void Write2DArrayInt(short int **Array, int Columns, int Rows, FILE
    > > *fp)
    > > {

    >
    > > int i;

    >
    > > for(i=0; i<Rows; i++){
    > > fwrite(Array, sizeof(short int),Columns, fp);
    > > }
    > > fclose(fp);

    >
    > > }

    >
    > > And tcumulus is defined as : short **tcumulus

    >
    > Is it possible that this is being called on a file that's already
    > closed? It's a little odd for a function to close a file which it
    > didn't open, so there could be a bug lurking.
    >
    > Otherwise this looks fine, so the bug could be somewhere else in your
    > code. It is common for malloc() errors to go undetected until much
    > later in the program's execution.


    Thank u~ Actually I called the function Write2DArrayInt for 10 times
    and it was killed at the second one ....:(
     
    xiao, Aug 8, 2008
    #3
  4. On Thu, 7 Aug 2008 18:22:51 -0700 (PDT), xiao <>
    wrote:

    >Hey ~ everyone~
    >What does this error mean?
    >*** glibc detected *** double free or corruption (!prev):
    >
    >It occurs when i tried to write to a file like this :
    >
    >Write2DArrayInt(tcumulus, ncolumns,nrows, out);
    >
    >Write2DArrayInt is a function like this:
    >
    >void Write2DArrayInt(short int **Array, int Columns, int Rows, FILE
    >*fp)
    >{
    >
    > int i;
    >
    > for(i=0; i<Rows; i++){
    > fwrite(Array, sizeof(short int),Columns, fp);
    > }
    > fclose(fp);
    >}
    >
    >And tcumulus is defined as : short **tcumulus


    The two common reasons for that error message are 1) passing the same
    address to free twice and 2) overrunning an allocated area before
    passing its address to free.

    Chances are you do one or the other in the code you haven't shown us.
    You need to provide a complete, compilable, and preferably short
    example that demonstrates the undesirable behavior.

    --
    Remove del for email
     
    Barry Schwarz, Aug 8, 2008
    #4
  5. xiao

    xiao Guest

    Re: what does double free or corruption (!prev): mean? Thank u~

    On Aug 7, 9:39 pm, Barry Schwarz <> wrote:
    > On Thu, 7 Aug 2008 18:22:51 -0700 (PDT), xiao <>
    > wrote:
    >
    >
    >
    > >Hey ~ everyone~
    > >What does this error mean?
    > >*** glibc detected *** double free or corruption (!prev):

    >
    > >It occurs when i tried to write to a file like this :

    >
    > >Write2DArrayInt(tcumulus, ncolumns,nrows, out);

    >
    > >Write2DArrayInt is a function like this:

    >
    > >void Write2DArrayInt(short int **Array, int Columns, int Rows, FILE
    > >*fp)
    > >{

    >
    > > int i;

    >
    > > for(i=0; i<Rows; i++){
    > > fwrite(Array, sizeof(short int),Columns, fp);
    > > }
    > > fclose(fp);
    > >}

    >
    > >And tcumulus is defined as : short **tcumulus

    >
    > The two common reasons for that error message are 1) passing the same
    > address to free twice and 2) overrunning an allocated area before
    > passing its address to free.
    >
    > Chances are you do one or the other in the code you haven't shown us.
    > You need to provide a complete, compilable, and preferably short
    > example that demonstrates the undesirable behavior.
    >
    > --
    > Remove del for email

    hm....i think i have figure it out ,because i closed the file at the
    end of the function, then , I call the function again. SO I deleted
    this line in the function : fclose(fp); and then, it works. DO u
    think that is the reason?
     
    xiao, Aug 8, 2008
    #5
  6. xiao

    Default User Guest

    Re: what does double free or corruption (!prev): mean? Thank u~

    xiao wrote:

    > On Aug 7, 9:39 pm, Barry Schwarz <> wrote:
    > > On Thu, 7 Aug 2008 18:22:51 -0700 (PDT), xiao
    > > <> wrote:
    > >
    > >
    > >
    > > > Hey ~ everyone~
    > > > What does this error mean?
    > > > *** glibc detected *** double free or corruption (!prev):

    > >
    > > > It occurs when i tried to write to a file like this :

    > >
    > > > Write2DArrayInt(tcumulus, ncolumns,nrows, out);

    > >
    > > > Write2DArrayInt is a function like this:

    > >
    > > > void Write2DArrayInt(short int **Array, int Columns, int Rows,
    > > > FILE *fp)
    > > > {

    > >
    > > > int i;

    > >
    > > > for(i=0; i<Rows; i++){
    > > > fwrite(Array, sizeof(short int),Columns, fp);
    > > > }
    > > > fclose(fp);
    > > > }

    > >
    > > > And tcumulus is defined as : short **tcumulus

    > >
    > > The two common reasons for that error message are 1) passing the
    > > same address to free twice and 2) overrunning an allocated area
    > > before passing its address to free.
    > >
    > > Chances are you do one or the other in the code you haven't shown
    > > us. You need to provide a complete, compilable, and preferably
    > > short example that demonstrates the undesirable behavior.
    > >
    > > --
    > > Remove del for email

    > hm....i think i have figure it out ,because i closed the file at the
    > end of the function, then , I call the function again. SO I deleted
    > this line in the function : fclose(fp); and then, it works. DO u
    > think that is the reason?


    Did you open the file again before calling the function the second time?


    This is why you should provide a COMPLETE minimal program instead of
    snippets.




    Brian
     
    Default User, Aug 8, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,505
    Victor Bazarov
    Feb 17, 2005
  2. Grant Edwards

    glibc detected double free or corruption

    Grant Edwards, Dec 20, 2006, in forum: Python
    Replies:
    0
    Views:
    429
    Grant Edwards
    Dec 20, 2006
  3. Delaney, Timothy (Tim)

    RE: glibc detected double free or corruption

    Delaney, Timothy (Tim), Dec 20, 2006, in forum: Python
    Replies:
    0
    Views:
    526
    Delaney, Timothy (Tim)
    Dec 20, 2006
  4. chuan
    Replies:
    2
    Views:
    3,100
    James Kanze
    Nov 15, 2008
  5. aspineux
    Replies:
    2
    Views:
    665
    Gabriel Genellina
    Dec 29, 2008
Loading...

Share This Page