LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   *** glibc detected *** double free or corruption - ERROR (http://www.linuxquestions.org/questions/programming-9/%2A%2A%2A-glibc-detected-%2A%2A%2A-double-free-or-corruption-error-538498/)

bandwidthjunkie 03-18-2007 09:32 AM

*** 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.

ntubski 03-18-2007 11:40 AM

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>]


bandwidthjunkie 03-19-2007 09:36 AM

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.


All times are GMT -5. The time now is 06:23 AM.