Cpper.Lee Cpper.Lee - 3 months ago 8
C++ Question

It is safe to call a member function on a vtkObject after I call Delete function?

The following code comes from vtkAbstractTransform. My application crushed in this function, Is this code safe?

void vtkTransformConcatenation::Concatenate(const double elements[16])
{
// concatenate the matrix with either the Pre- or PostMatrix
if (this->PreMultiplyFlag)
{
if (this->PreMatrix == NULL)
{
// add the matrix to the concatenation
vtkSimpleTransform *mtrans = vtkSimpleTransform::New();
this->Concatenate(mtrans);
mtrans->Delete(); // call Delete on mtrans
this->PreMatrixTransform = mtrans;
this->PreMatrix = mtrans->GetMatrix();
}
vtkMatrix4x4::Multiply4x4(*this->PreMatrix->Element, elements,
*this->PreMatrix->Element); // My application crushed here.
...
} }

Answer

Conceptually it doesn't make sense to call the function over deleted object. After deleting the object this assignment

 this->PreMatrixTransform = mtrans;

usually had put garbage values (possibly NULL value) in this->PreMatrixTransform. and when this variable is used (I am pretty sure) it will cause crash.

Comments