I am trying to overload the assignment operator to do a deep copy of a polygon object, the program compiles but I am getting an error toward the end that I want to clear up. Below is the relevant code, if you think I need to add more please just post a comment. Assume the proper #include
's and that the <<
operator is overloaded for proper output etc...
The error is: malloc: * error for object 0x1001c0: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug.
//Polygon.h
// contains two classes PolygonNode and Polygon
class PolygonNode //Used to link points in a polygon so that they can be iterated through in order
{
public:
...
methods etc
...
private:
Point pt_; // the points in the polygon are made using the Point class
PolygonNode* link_ ; // pointer to the next point in the polygon
};
class Polygon // Connects points and forms a polygon
{
public:
...
Polygon& operator= (Polygon ply);
void Polygon::addPoint(const Point &p);
// methods etc
...
private:
int numPoints_;
bool closed_polygon_;
PolygonNode* first_ ; // points to the first point of the polygon
PolygonNode* last_ ; // points to the last point of the polygon
};
//Polygon.cpp
...
PolygonNode::~PolygonNode()
{
delete link_ ; // possible problem area
}
Polygon::~Polygon()
{
delete first_ ; // possible problem area
last_ = NULL ;
}
void Polygon::addPoint(const Point &p)
{
PolygonNode* ptr ;
ptr = new PolygonNode(p) ;
if( last_ != NULL )
last_->setLink(ptr) ;
last_ = ptr ;
if( first_ == NULL )
first_ = last_ ;
numPoints_++ ;
}
Polygon& Polygon::operator= (const Polygon ply)
{
for (int i = 0; i < ply.numPoints()-1; i++)
{
addPoint(ply.getPoint(i));
}
if (ply.isClosed())
{
closePolygon();
}
else
{
addPoint(ply.getPoint(ply.numPoints()-1));
}
return this;
}
void Polygon::addPoint(const Point &p)
{
PolygonNode ptr ;
ptr = new PolygonNode(p) ;
if( last_ != NULL )
last_->setLink(ptr) ; // sets the last pointer to the new last point
last_ = ptr ;
if( first_ == NULL )
first_ = last_ ;
numPoints_++ ;
}
...
//main.cpp
Polygon ply;
...
Point pt0(0,0);
Point pt1(1,1);
ply.addPoint(pt0);
cout << "ply = " << ply << endl;
Polygon newply;
newply = ply; // use of the assignment operator
cout << "Polygon newply = ply;" << endl;
cout << "newply = " << newply << endl;
cout << "ply = " << ply << endl;
newply.addPoint(pt1);
cout << "newply.addPoint(Point(0,0)); " << endl;
cout << "newply = " << newply << endl;
cout << "ply = " << ply << endl;
...
I have read elsewhere that this is possibly due to a bug in OS 10.6 or Xcode 3.2 if there is a workaround could someone please give me detailed instructions for how to do the workaround, I do not have a lot of experience with Xcode.
Edited: added parts of code that use delete
, notice that it is being used in the destructors for Polygon and PolygonNode
Edited: added the part of the code where link_ is allocated, setLink is a simple setter method.