LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-07-2008, 08:36 PM   #1
nanxy
LQ Newbie
 
Registered: Jul 2008
Posts: 16

Rep: Reputation: 0
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
 
Old 08-07-2008, 10:15 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-08-2008, 04:44 AM   #3
Aidin_36
LQ Newbie
 
Registered: Jun 2005
Location: Iran
Distribution: Debian
Posts: 24

Rep: Reputation: 15
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.
 
Old 08-08-2008, 03:15 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-09-2008, 07:17 PM   #5
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
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.
 
Old 08-10-2008, 12:15 AM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by nanxy View Post
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");
 
Old 08-10-2008, 11:26 AM   #7
nanxy
LQ Newbie
 
Registered: Jul 2008
Posts: 16

Original Poster
Rep: Reputation: 0
Thank u very much , guys~
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent: glibc detected *** double free or corruption error when creating ms_newMapobj SteevN Linux - General 0 04-01-2008 07:52 AM
glibc double free or corruption lamtab Programming 2 01-25-2008 03:21 AM
*** glibc detected *** double free or corruption - ERROR bandwidthjunkie Programming 2 03-19-2007 09:36 AM
Open Office user install error: *** glibc detected *** double free or corruption: 0xb r_jensen11 Linux - General 6 01-16-2005 06:08 AM


All times are GMT -5. The time now is 08:59 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration