*** glibc detected *** double free or corruption (C++)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
*** glibc detected *** double free or corruption (C++)
Ok, I'm a relative C++ newb, but I have some idea what this means:
"*** glibc detected *** double free or corruption (!prev): 0x08054810 ***". The strange thing is I don't understand why the particular line I have isolated causes it. The error prints while my program is part way through executing and kills it. By process of excessive printf statements, the following line appears to be the cause :
Code:
void BestOfList::additem(Calander toadd) {
int score = toadd.score();
size_t len = Scores.size();
if(score < 0) {return;}
printf("Adding to list will continue - score >= 0\n");
// Calander is failed or just very bad.
for(unsigned int i = 0; i < len; i++) {
printf("Checking to see if calander toadd is the same calander %u\n",i);
if(score == Scores[i]) {
if(toadd == Calanders[i]) {
printf("Calander is the same. returning...\n");
return;
}
}
}
// We already have calander in the list because the
// same calander can be created multiple ways
printf("toadd passed required checks. adding Calander...\n");
Scores.push_back(score);
printf("pushed back score\n");
Calanders.push_back(toadd); // <- Line causing problem
printf("pushed back calander\n");
reorder();
resize();
}
This is part of my BestOfList class:
Code:
class BestOfList {
private:
vector<int> Scores;
vector<Calander> Calanders;
void resize();
void reorder();
public:
void additem(Calander toadd);
int getscore(unsigned int index);
Calander getcalander(unsigned int index);
unsigned int length();
void empty();
};
Snip of stdout/stderr from execution:
Code:
(snip)
Calander is not a failure. Continuing test.
Returning score of 60000
Adding to list will continue - score >= 0
toadd passed required checks. adding Calander...
pushed back score
pushed back calander
Reordering calander...
Resizing Calander...
Appointment at 0,14
Adding appointment 3
(snip)
Adding to list will continue - score >= 0
Checking to see if calander toadd is the same calander 0
toadd passed required checks. adding Calander...
pushed back score
*** glibc detected *** double free or corruption (!prev): 0x08054810 ***
Looking at the dump, it must be some unusual condition - it works at least once before screwing up.
Any Ideas? Even if I knew better what might cause a vector to double-free, I might have a better idea of what is wrong with it...
Bit of a guess here but I think that you want the toadd argument to be passed in as a reference, otherwise it will just be a copy object and deleted when the method finishes ... maybe ?
The way I understand it is that push_down() doesn't make a copy but a reference. Which was why I feel it would be safer to at least pass toadd as a reference. But as I said that is just a guess.
The way I understand it is that push_down() doesn't make a copy but a reference. Which was why I feel it would be safer to at least pass toadd as a reference. But as I said that is just a guess.
graeme.
i dont think so.. (see ex at bottom) the way urzumph has created it push_back will store a copy... as for the problem urzumph,
i would consider compiling it if you wanted to upload the code..
i may see somthing,, maybe not.. worth a shot..
but i would like to see where it is coming from also as it does not appear to make sense at this time..
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class foo{
public:
foo(){ lock=1; name=""; }
void nameFoo(string n) { name=n; }
void unlock() { lock=0; }
void print(){ if(!lock) cout << "in a foo: " << name << endl; }
int lock;
string name;
};
class foolist{
public:
vector<foo> v;
void add(foo f) { v.push_back(f); }
void list(){
for(int i=0; i<v.size(); ++i){
v[i].print();
}
}
};
/* the copy in here will be pushed onto the list, and can be read only from list when it goes out of scope.. */
void bar(foolist& l){
foo temp; // this foo is local scope and should DIE
temp.unlock(); // unlock it so we can be sure it is this one being printed
temp.nameFoo("dead Foo?"); // name it too just for fun
l.add(temp); // push it onto the list from main.
}
int main(){
foolist l;
foo f;
f.unlock(); // unlock it so we can be sure it is this foo
f.nameFoo("main's Foo"); // name it
l.add(f); // add one foo here
bar(l); // add another foo that goes out of scope here..
l.list(); // shows two foos here
return 0;
}
xhi that looks correct when I tried it, but is what is happening what you think?
if you add a destructor to the class foo like:
Code:
~foo()
{
cout <<name <<" out of scope\n";
}
before the code exits from main the output is
main's Foo out of scope
main's Foo out of scope
main's Foo out of scope
dead Foo? out of scope
dead Foo? out of scope
dead Foo? out of scope
in a foo: main's Foo
in a foo: dead Foo?
whats happening here?
<edit>
Code:
void add(foo f) { v.push_back(f); }
this func is using pass by value and therefore creating its own
copy.
</edit>
I changed the code to pass the f into the method add() by reference. The destructor showed a total of five object being destroyed.
I then changed it to pointers and it was back to the expected three
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class foo{
public:
foo(){ lock=1; name=""; }
~foo(){cout <<name <<" out of scope\n";}
void nameFoo(string n) { name=n; }
void unlock() { lock=0; }
void print(){ if(!lock) cout << "in a foo: " << name << endl; }
int lock;
string name;
};
class foolist{
public:
vector<foo*> v;
void add(foo* f) { f->nameFoo("in foolist"),v.push_back(f); }
void list(){
for(int i=0; i<v.size(); ++i){
v[i]->print();
}
}
};
/* the copy in here will be pushed onto the list, and can be read only from list when it goes out of scope.. */
void bar(foolist& l){
foo temp; // this foo is local scope and should DIE
temp.unlock(); // unlock it so we can be sure it is this one being printed
temp.nameFoo("dead Foo?"); // name it too just for fun
l.add(&temp); // push it onto the list from main.
temp.nameFoo("dead Foo?"); // name it too just for fun
}
int main(){
foolist l;
foo f;
f.unlock(); // unlock it so we can be sure it is this foo
f.nameFoo("main's Foo"); // name it
l.add(&f); // add one foo here
f.nameFoo("main's Foo"); // name it
bar(l); // add another foo that goes out of scope here..
l.list(); // shows two foos here
return 0;
}
Output:
Code:
dead Foo? out of scope
in a foo: main's Foo
in a foo: dead Foo?
main's Foo out of scope
Press Enter to continue!
Okay I created a copy constructor and wrapped the push_back with diagnostics
It was creating a copy for the object when it is passed into the add() method and then creating a copy when it is pushed onto the vector
When the second element is pushed onto the vector the original element is copied the vector is then populated with new copies and the original copy destroyed.
I added a third element and got three copies and two destroys
Thinking I saw a pattern I added the original foo object f. This resulted in a single copy no destroys.
Added a fifth object and it was back to the original pattern of 5 copies and four destroys and so I think this is how the memory is allocated for the vector.
You create a vector it will have sufficient memory for one element.
Add a second element then it will need to allocate more memory.
Now here is the speculation... It wants the memory to be contiguous and so it recreates itself and copies the data across. Pointers to objects shouldn't be a problem but references cause the copy constructor to kick in.
great work graemef..
i knew this but had locked away in my mind (behind some bong resin probably)..
this is from thinking in c++ vol2
Code:
Vector
Cost of overflowing allocated storage
A vector starts by grabbing a block of storage, as if it’s taking a guess at how many objects
you plan to put in it. As long as you don’t try to put in more objects than can be held in the
initial block of storage, everything is very rapid and efficient (note that if you do know how
many objects to expect, you can pre-allocate storage using reserve( )). But eventually you
will put in one too many objects and, unbeknownst to you, the vector responds by:
1. Allocating a new, bigger piece of storage
2. Copying all the objects from the old storage to the new (using the copy-constructor)
3. Destroying all the old objects (the destructor is called for each one)
4. Releasing the old memory
For complex objects, this copy-construction and destruction can end up being very expensive
if you overfill your vector a lot. To see what happens when you’re filling a vector, here is a
class that prints out information about its creations, destructions, assignments and copyconstructions:
so thats what was causing our unexpected results.. ??
use vector::reserve(int) to init the vectors storage.. not number of elements.. but contiguous storage available..
add this constructor to my code..
oh yeh... here is the current output.. maybe i have just been looking too hard.. but i dont see where the top two are coming from?
Code:
main's Foo out of scope <--- ???
dead Foo? out of scope <--- ???
dead Foo? out of scope
in a foo: main's Foo
in a foo: dead Foo?
main's Foo out of scope
in foolist::~foolist()
main's Foo out of scope
dead Foo? out of scope
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.