Explore what will happen when we use delete on vtkSmartPointer<vtkActor> object.

    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.

The delete function free allocated memory but the internal pointer is not null. vtkSmartPointer try to handle memory that has bee freed. So it crashed once we try to close the program.

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
Categories: VTK

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page