I made a function for an object called copy() that should just return an instance of the object with all the same values –
Grid Grid::copy() {
Grid result;
result.setFilename(f_name);
result.setNumOfRows(num_rows);
result.setNumOfCols(num_cols);
result.setMap(map);
return result;
}
My destructor looks like this –
Grid::~Grid() {
for(int r=0;r<num_rows;r++)
delete [] map[r];
}
Now whenever my code is running and the copy function gets called, I get an error
*** glibc detected *** ./go: double free or corruption (!prev): 0x0982c6a8 ***
with a lot of other information (big wall of text) after that. That just means the memory is being deleted twice correct? If so, how can this be? Why does the destructor get called twice?
The code where it gets called looks like this –
for(;;) {
Grid g;
if(which_display == 1) {
.....
.....
g = myServer->getAgent()->getGrid()->copy(); //HERE
}
//print
std::cout<<g.toString();
}
I feel like I’m missing something obvious. Can someone point out to me how the destructor is being called twice?
You don’t actually want a
copymethod at all. You simply want a copy constructor and assignment operator. I’m guessing your line originally looked like this:And since that didn’t work, you added the copy method. But the way it is now, fixing your copy method alone will not solve the problem, as you need the copy constructor and assignment operator too, or your hard work in fixing the copy method could be destroyed.
First, a quick explanation as to what is happening, and why the program fails:
copyGridon the stack.Grid, but we suspect it does a shallow copy.Grid. *Grid‘s destructor fires, deleting the contents ofmap.Grid, but one that points to deleted memory.Gridobject is assigned intog. This invokes the assignment operator ofGrid. *map— which were already deleted. boom.ggoes out of scope, its destructor will try to delete the contents ofmapyet again.As you can see, there are 3 places where a shallow copy occurs — all must be fixed or this will still fail.
How to fix this
setMapandsetFilenameto do a deep copy.Here’s what the assignment operator could look like (assuming all set methods do deep copy):
There are techniques to write the copy constructor, and then make the assignment operator use the copy constructor. This is a good technique (less duplicated code), but I don’t have the link handy. When I find it, I’ll link it here.
Lastly, I marked a few lines in my explanation(*). Compilers can do Return Value Optimization (RVO), and Named RVO. With these optimizations, it would not actually create the
Gridobject on the stack withincopy, and then copy-construct for the return value — it would simply create the temporary object for the result ofcopy, and then thecopymethod would use that instead of its own internal stack-basedGridobject. So with enough compiler optimizations, your code might make it past this point and crash later. Obviously that’s not helpful, so this is more of an fyi.