A: 

You should never, ever use raw pointers unless in a dedicated class, normally. Change them to a smart pointer (auto or shared will do in this case) and stop having to free your own memory -> problem solved. Edit:

The smarter option is just to use a std::list or std::vector.

DeadMG
+2  A: 

I can't see the constructor for the PolygonNode class. Is the link_ pointer initialized to null on creation? If not, that may be the problem manifesting itself in the error you get. You have to make sure, the link_ pointers in the PolygonNode instances get initialized to null. Define appropriate constructors.

Do you have a copy constructor defined for your polygon class? I can't see one in the code posted, but maybe you just didn't paste it and you have one. If not, that is one of possible sources of serious problems.

The copy constructor, that gets synthesized automatically by the compiler will just copy the pointers in the Polygon class.

Your assignment operator takes the argument by value

Polygon& operator= (Polygon ply);

This makes use of the copy constructor. If it's the automatically synthesized one, ply inside the operator has pointers pointing to the same list, the argument passed by value to the operator owns. ply behaves like it owned the list too and the list gets destroyed when ply goes out of scope. The original argument is left with dangling pointers.

You should define correct copy constructor.

You should also consider taking the argument in the assignment operator by const reference. I don't see a reason to take it by value. Maybe you have one, but even if you do, you can change it temporarily, to test the operator, before you define correct copy constructor. In your operator you should check for self-assignment. All I can see now is adding new nodes to the old Polygon. I don't think it's right, but I guess it's just for testing now.

Maciej Hehl
@Maciej: link_ is initialized to `NULL` that was a good thought though.
Jordan
@Jordan So how about copy constructor? If you don't have one defined properly, some objects get deleted twice. Copy constructor is probably as complicated as the assignment operator, maybe a little bit less. If you want to test the operator first, before copy constructor, change the signature of your operator, to pass the argument by reference (const). This way copy constructor won't be used.
Maciej Hehl
@Maciej: I tried implementing the copy constructor the way I implemented the assignment operator (except I didn't `return *this;`) and it worked. Thank you for your help
Jordan
A: 

I think the problem is the link_ variable, it's not allocated in your example and never used...

PC2st