You don't actually want a copy
method at all. You simply want a copy constructor and assignment operator. I'm guessing your line originally looked like this:
g = myServer->getAgent()->getGrid();
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:
- You call
copy
- This enters your copy method, which creates a
Grid
on the stack.
- It sets the members of the
Grid
, but we suspect it does a shallow copy.
- The copy method returns, which invokes the copy constructor of
Grid
. *
- The default copy constructor does a shallow copy.
- The stack-based
Grid
's destructor fires, deleting the contents of map
.
- The copy method has now returned, providing a temporary
Grid
, but one that points to deleted memory.
- Now the temporary
Grid
object is assigned into g
. This invokes the assignment operator of Grid
. *
- The default assignment operator does a shallow copy, just like the default copy constructor did.
- At the end of the line, the temporary object is destroyed, where it tries to delete the contents of
map
-- which were already deleted. boom.
- When
g
goes out of scope, its destructor will try to delete the contents of map
yet 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
- Get rid of the copy method -- it doesn't provide any value anyway.
- Fix your
setMap
and setFilename
to do a deep copy.
- Create an assignment operator. This should deep-copy the contents of the other Grid.
- Create a copy constructor, just like the assignment operator.
Here's what the assignment operator could look like (assuming all set methods do deep copy):
Grid& operator= (const Grid& g) {
setFilename(f_name);
setNumOfRows(num_rows);
setNumOfCols(num_cols);
setMap(map);
return *this;
}
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 Grid
object on the stack within copy
, and then copy-construct for the return value -- it would simply create the temporary object for the result of copy
, and then the copy
method would use that instead of its own internal stack-based Grid
object. 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.