A very good example why this is a problem can be found in mitkPlanarFigureIOTest.cpp:
static PlanarFigureList CreateDeepCopiedPlanarFigures(PlanarFigureList original) { PlanarFigureList copiedPlanarFigures; PlanarFigureList::iterator it1; for ( it1 = original.begin(); it1 != original.end(); ++it1 ) { mitk::PlanarFigure::Pointer copiedFigure; if(strcmp((*it1)->GetNameOfClass(), "PlanarAngle") == 0) { copiedFigure = mitk::PlanarAngle::New(); } // ... // ... // ... if(strcmp((*it1)->GetNameOfClass(), "PlanarFourPointAngle") == 0) { copiedFigure = mitk::PlanarFourPointAngle::New(); } copiedFigure->DeepCopy((*it1)); copiedPlanarFigures.push_back(copiedFigure.GetPointer()); } return copiedPlanarFigures; }
The function should look more like:
static PlanarFigureList CreateClonedPlanarFigures(PlanarFigureList original) { PlanarFigureList clonedPlanarFigures; clonedPlanarFigures.resize(original.size()); std::transform(original.begin(), original.end(), clonedPlanarFigures.begin(), Clone); return clonedPlanarFigures; }
We should use the newly introduced clone API of ITK since PlanarFigure is a BaseData.
Solution: Add mitkCloneMacro(Self) in the protected sections of all classes that derive from PlanarFigure (forced by base class). If something has to be deep copied, explicitly implement the copy constructor in addition.