Access violation when deleting actor made from GLTF importer

Hello,

My application have to load different scenes repeatedly (synoptic views for SCADA application).
I need to clean-up properly the renderer between to scenes to avoid increasing memory usage.
I use a loop to clean-up like this:

vtkActorCollection* actors = vtkActorCollection::New();
actors = renderer->GetActors();
actors->InitTraversal();
vtkActor nextActor = actors->GetNextActor();
while (nextActor != NULL)
{
renderer->RemoveActor(nextActor);
nextActor->Delete(); // this line with access viloation
nextActor = actors->GetNextActor();
}

it works fine with simple objetcs (cubes…) or OBJ imported files but raise an “access violation” on the delete line if objects are loaded with GLTF importer.

is it a bug or there is another way to clean-up the scene ?
I use the 8.9.0 version of the VTK library.

Thanks for help.

You should not delete the actor, removing it is enough. You can also compile VTK with VTK_DEBUG_LEAKS CMake option to help you with the management of VTK resources.
It also looks like you will have a leak here on the vtkActorCollection object.
Try this:

vtkActorCollection* actors = renderer->GetActors();
actors->InitTraversal();
vtkActor nextActor = actors->GetNextActor();
while (nextActor != nullptr)
{
  renderer->RemoveActor(nextActor);
  nextActor = actors->GetNextActor();
}

Alternatively, you can simply try: renderer->GetActors()->RemoveAllItems();

1 Like

It works now without deleting actor (RemoveAllItems() is nice).

Thanks for your help.

Sorry for my previous reply. RemoveAllItems doesn’t work. It doesn’t remove the actor from the renderer.
The loop is correct.

vtkActorCollection* actors = renderer->GetActors();
actors->InitTraversal();
vtkActor* nextActor = actors->GetNextActor();
while (nextActor != NULL)
	 {
	 renderer->RemoveActor(nextActor);
	 nextActor = actors->GetNextActor();
	 }
1 Like