# Access violation when deleting actor made from GLTF importer

**URL:** https://discourse.vtk.org/t/access-violation-when-deleting-actor-made-from-gltf-importer/2400
**Category:** Support
**Created:** [January 10, 2020, 1:38pm UTC](https://discourse.vtk.org/t/access-violation-when-deleting-actor-made-from-gltf-importer/2400 "2020-01-10T13:38:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pbrousson](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@pbrousson](https://discourse.vtk.org/u/pbrousson)
#### Post date: [January 10, 2020, 1:38pm UTC](https://discourse.vtk.org/t/access-violation-when-deleting-actor-made-from-gltf-importer/2400/1 "2020-01-10T13:38:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [January 10, 2020, 3:13pm UTC](https://discourse.vtk.org/t/access-violation-when-deleting-actor-made-from-gltf-importer/2400/2 "2020-01-10T15:13:04Z")

</div>

> [@pbrousson](#):
>
> 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();  
> }

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();`

---

<div class="post-metadata">

### Author: ![pbrousson](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@pbrousson](https://discourse.vtk.org/u/pbrousson)
#### Post date: [January 10, 2020, 4:29pm UTC](https://discourse.vtk.org/t/access-violation-when-deleting-actor-made-from-gltf-importer/2400/3 "2020-01-10T16:29:34Z")

</div>

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

Thanks for your help.

---

<div class="post-metadata">

### Author: ![pbrousson](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@pbrousson](https://discourse.vtk.org/u/pbrousson)
#### Post date: [January 10, 2020, 5:32pm UTC](https://discourse.vtk.org/t/access-violation-when-deleting-actor-made-from-gltf-importer/2400/4 "2020-01-10T17:32:02Z")

</div>

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();
	 }
```
