LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 03-18-2007, 09:32 AM   #1
bandwidthjunkie
LQ Newbie
 
Registered: Jan 2007
Location: london
Distribution: Gentoo(amd64) - 2.16.20
Posts: 26

Rep: Reputation: 15
*** glibc detected *** double free or corruption - ERROR


This isn't a question, but I thought it might be helpful as I have found quite a few posts on the net about this problem and I have spent the last hour and a half fixing it.

PROBLEM:

vector<MyClass> myVector;

for(int i = 1; i != 11 ; ++i)
{
MyClass* myObject = new MyClass();
myVector.push_back(*myObject);
delete myObject;
}

======

It compiles fine, then bombs out after a few loops saying
" *** glibc detected *** double free or corruption "
and dumps a load of junk to the terminal.

SOLUTION:

There are missing or incorrect copy or assignment operators for the MyClass class, or some of the sub-classes of MyClass. Make sure that for every class you define:

MyClass(const MyClass& object);
MyClass& operator=(const MyClass& rhs);

Telling the program exactly how to deep copy objects, if you don't do this then it is only the pointers that get copied and this leads to big problems. (Also remember to make sure that your destructor properly deletes all pointers.

======

Hope that helps someone.

Last edited by bandwidthjunkie; 03-19-2007 at 09:31 AM.
 
Old 03-18-2007, 11:40 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
hmm, maybe you didn't type your program in right, because that shouldn't compile, you're trying to push a MyClass pointer (MyClass*) into a vector of MyClass objects.

Code:
~/tmp% cat free.cc                                                                                    
#include <vector>
using namespace std;
class MyClass {};

int main() {
    vector<MyClass> myVector;

    for(int i = 1; i != 11 ; ++i)
    {
        MyClass* myObject = new MyClass();
        myVector.push_back(myObject); //<-- line 11
        delete myObject;
    }
}
~/tmp% g++ free.cc                                                                                    
free.cc: In function 'int main()':
free.cc:11: error: no matching function for call to 'std::vector<MyClass, std::allocator<MyClass> >::push_back(MyClass*&)'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = MyClass, _Alloc = std::allocator<MyClass>]
 
Old 03-19-2007, 09:36 AM   #3
bandwidthjunkie
LQ Newbie
 
Registered: Jan 2007
Location: london
Distribution: Gentoo(amd64) - 2.16.20
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ntubski
hmm, maybe you didn't type your program in right, because that shouldn't compile, you're trying to push a MyClass pointer (MyClass*) into a vector of MyClass objects.
You are right I forgot the * in the code above (I have edited it now), it was just an example and what I had written wouldn't have compiled. My situation was that the code compiled, but I got this "double free or corruption error" - I noticed that there were a few long meandering and not very useful threads on various sites about this, but none of them seemed to give an answer to the problem so I thought I would post one here so it might be easy to find from a google search.
 
  


Reply



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
FC3 glibc detected *** double free or corruption: josedragone Fedora 5 09-17-2009 10:16 PM
glibc detected double free or corruption (URGENT) dayalan_cse Programming 4 10-30-2006 03:23 PM
*** glibc detected *** double free or corruption (!prev): 0x08294448 *** Deepti Joshi Linux - Software 1 05-31-2006 10:55 PM
*** glibc detected *** double free or corruption (C++) urzumph Programming 25 01-07-2006 04:03 PM
Open Office user install error: *** glibc detected *** double free or corruption: 0xb r_jensen11 Linux - General 6 01-16-2005 06:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:58 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration