LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   What does this error mean? double free or corruption (https://www.linuxquestions.org/questions/programming-9/what-does-this-error-mean-double-free-or-corruption-661294/)

nanxy 08-07-2008 08:36 PM

What does this error mean? double free or corruption
 
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[i], sizeof(short int),Columns, fp);
}
fclose(fp);

}

And tcumulus is defined as : short **tcumulus

ta0kira 08-07-2008 10:15 PM

You provide a FILE* to Write2DArrayInt and it closes the file, but does your code try to close the same file somewhere else? That's generally how that error happens in your situation.
ta0kira

Aidin_36 08-08-2008 04:44 AM

You can disable this by disabling malloc check. use this command:

$export MALLOC_CHECK_=0

And then run your program.
But you may receive some strange bugs. try to fix the code.

ta0kira 08-08-2008 03:15 PM

Though the error is annoying, disabling the message isn't a good solution. As far as I know, this never comes up when it isn't true.
ta0kira

raconteur 08-09-2008 07:17 PM

Using code tags in the forum will help us (and you) read these sorts of things more easily.

Since compilers differ widely in their initialization of declared variables, it is always good practice to initialize them, and to always check and/or assign them when they are used.

For example:
Code:

  FILE *fp = NULL;

  if(fp == NULL)
    fp = fopen(/* ... */);

  if(fp == NULL)
    /* error handler */
  else
    /* do the work */

  fclose(fp);
  fp = NULL;

In general:
-- when you declare a variable, initialize it (or, at least, initialize it as early as possible)
-- before you use a potentially NULL or otherwise uninitialized variable, check its state
-- when you perform an action that may leave the variable in an undetermined state, initialize it to a known state

This is always good programming practice and will save you aggravation and confusion.

ErV 08-10-2008 12:15 AM

Quote:

Originally Posted by nanxy (Post 3240427)
Hey ~ everyone~
What does this error mean?
*** glibc detected *** double free or corruption (!prev):

Can happen because:
1) You are calling fclose two times in a row. Closing file in a function where it was passed as parameter isn't a good idea. You should close files in functions where they were opened.

2) You are freeing previously allocated memory several times. For example:
Code:

//in C++
int *i = new int;
delete i;
delete i;

or
Code:

/*in C*/
char * c;
c = (char*)malloc(4);
free(c);
free(c);

3)You are writing to memory outside of allocated regions, like this:
Code:

//in C++
const char* c = "123456789";
char* c1 = new char[4];
strcpy(c1, c);

or
Code:

/*in C*/
char * c;
c = malloc(4);
strcpy(c, "123456789");


nanxy 08-10-2008 11:26 AM

Thank u very much , guys~


All times are GMT -5. The time now is 12:53 PM.