Cpper.Lee Cpper.Lee - 1 year ago 77
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();
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download