Danger of destructors: objects are copied in many situations.
We need to make sure that the copies are deep copies
and not shallow copies. Otherwise, when a destructor is called
it frees the memory that other objects are still pointing to.
Examples of bad behavior:
-
Using delete instead of delete [] :
bad1.cpp.
-
After assignment:
bad2.cpp.
-
After initializing with DMC X = DMC(Y) :
bad3.cpp.
Note: default copy constructor called instead of
default constructor.
-
Passing a DMC by value:
bad4.cpp.
-
Returning a DMC value:
bad5.cpp.
-
Explicitly calling a destructor:
bad6.cpp.
-
Explicitly calling a destructor thru a pointer:
bad7.cpp.