Use vtkObjectBase’s Function Delete To Free Object
Explore what will happen when we use delete on vtkSmartPointer object.
// An highlighted block
vtkSmartPointer<vtkActor> leftActor =
vtkSmartPointer<vtkActor>::New();
leftActor->SetMapper( mapper );
//...
void *ptr = leftActor.Get();
leftActor->GetProperty()->SetColor( 1, 0, 0 );
rightActor->GetProperty()->SetColor( 0, 1, 0 );
leftActor->Delete();
cout << "leftActor: " << (void *)leftActor.Get() << endl;
cout << "leftActor reference count: " << leftActor->GetReferenceCount() << endl;
if( ptr && leftActor->GetReferenceCount() > 0 )
{
leftRenderer->AddActor( leftActor );
}
if( rightActor )
{
rightRenderer->AddActor( rightActor );
}
output:
leftActor: 0x7ff976417b00
leftActor reference count: -1073741824
17:07:04: The program has unexpectedly finished.
17:07:04: The process was ended forcefully.
Remove vtkSmartPointer In the code snippet:
vtkActor *leftActor = vtkActor::New();
//...
leftActor->GetProperty()->SetColor( 1, 0, 0 );
rightActor->GetProperty()->SetColor( 0, 1, 0 );
while( leftActor->GetReferenceCount() > 0 )
{
leftActor->Delete();
}
leftActor = nullptr;
cout << "leftActor: " << (void *)leftActor << endl;
if( nullptr != leftActor && leftActor->GetReferenceCount() > 0 )
{
cout << "leftActor reference count: " << leftActor->GetReferenceCount() << endl;
leftRenderer->AddActor( leftActor );
}
output:
leftActor: 0x0