# System.AccessViolationException

**URL:** <https://discourse.vtk.org/t/system-accessviolationexception/11059>\
**Category:** Development\
**Created:** [March 27, 2023, 1:14pm UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059 "2023-03-27T13:14:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Micromegas](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/ce73a5/32.png) [@Micromegas](https://discourse.vtk.org/u/Micromegas)\
**Post date:** [March 27, 2023, 1:14pm UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/1 "2023-03-27T13:14:33Z")

</div>

Hallo @ll,

we are using VTK (Version: 9.0.1.1009) as Graphic API and have problems with a System.AccessViolationException : Attempt to read or write protected memory, which keeps occurring randomly, sometimes after 30 minutes or after 4 hours.  
I really don’t know how to work around this problem anymore.

The exception producing code is actually nothing special. The error occurs when we populate, or pass, a glyph with a source connection, which can be either an arrow or a sphere.  
glyph.SetSourceConnection(graphObj); // Line of Exception

At the given point it throws an exception, the vtkPolyData are filled with vtkPoints, but we have already checked them for validity. However, I don’t think this is our problem. The function we are using looks like this. Also, the way the Actor or PolyData is passed to the function doesn’t seem to matter (copy, ref, in: unimportant)

Code

```auto
 private void InitPolyData(
         ref vtkPolyData polyData,
         ref vtkPoints points,
         in string colorName,
         in vtkAbstractArray colorArray,
         in string velocityName,
         in vtkAbstractArray velocityArray
      )
      {
         this.InitPolyData(ref polyData, ref points, colorName, colorArray);

         // Add Velocity Array (points) to polydata
         polyData.GetPointData().AddArray(velocityArray);
         polyData.GetPointData().SetActiveVectors(velocityName);
      }

```

```auto
 private void InitActor(
         ref vtkActor actor,
         in bool isArrow,
         ref vtkPolyData inputData,
         in bool extendedArgs
      )
      {
         vtkGlyph3D glyph = new vtkGlyph3D();
         vtkPolyDataMapper mapperMoving = new vtkPolyDataMapper();
         vtkAppendPolyData polyDataListMoving = new vtkAppendPolyData();

         vtkAlgorithmOutput graphObj;
         if (isArrow)
         {
            /* Create Arrow geometry (sounds more complicated than it is :D) */
            vtkArrowSource arrowSource = new vtkArrowSource();
            arrowSource.SetShaftResolution(24);
            arrowSource.SetTipResolution(36);
            graphObj = arrowSource.GetOutputPort();
         }
         else
         {
            // Sphere for stationary points
            vtkSphereSource sphereSource = new vtkSphereSource();
            sphereSource.SetThetaResolution(20);
            sphereSource.SetPhiResolution(20);
            sphereSource.SetRadius(0.05);
            graphObj = sphereSource.GetOutputPort();
         }

         mapperMoving.SetColorModeToDefault();

         /* Create Actor for point cloud and add mapper */

         // actor = new vtkActor();
         actor.SetMapper(mapperMoving);

         /* Set point size */
         actor.GetProperty().SetPointSize(4);

         glyph.SetSourceConnection(graphObj);
         glyph.SetInputData(inputData);
         if (extendedArgs)
         {
            glyph.OrientOn();
            glyph.SetVectorModeToUseVector();
            glyph.SetScaleModeToScaleByVectorComponents();
         }

         glyph.SetColorModeToColorByScalar();
         glyph.SetScaleModeToDataScalingOff();
         glyph.SetScaleFactor(10);
         glyph.Update();

         polyDataListMoving.AddInputData(glyph.GetOutput());
         polyDataListMoving.Update();

         actor.GetMapper().SetInputDataObject(polyDataListMoving.GetOutput());
      }

```

Maybe someone knows this kind of error.

Thank you and best regards  
Emanuel

---

<div class="post-metadata">

**Author:** ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)\
**Post date:** [March 27, 2023, 1:22pm UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/2 "2023-03-27T13:22:50Z")

</div>

Are you monitoring application memory usage ? If I had to make a bet, there is a memory leak somewhere…

---

<div class="post-metadata">

**Author:** ![Micromegas](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/ce73a5/32.png) [@Micromegas](https://discourse.vtk.org/u/Micromegas)\
**Post date:** [March 28, 2023, 5:46am UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/3 "2023-03-28T05:46:05Z")

</div>

That was actually one of my first approaches, but a memory leak, has not been confirmed, if one exists, this is not something that is noticeable by profiling. It also doesn’t matter if you render 200 or 4000 points. The memory behavior does not change.  
We don’t use unsaved code in C#, so that doesn’t rule out memory leaks, but it makes them less likely.  
However, Access Violation is more likely to indicate uninitialized code, or perhaps even memory access that the program is not allowed. What exactly happens in the C++ code of VTK I cannot estimate, but it may not happen in the C# wrapper.  
I absolutely don’t know.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)\
**Post date:** [April 24, 2023, 3:14am UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/4 "2023-04-24T03:14:53Z")

</div>

Cc: @mwestphal @jfausty (not sure who to contact for VTK/C# support)

---

<div class="post-metadata">

**Author:** ![jfausty](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jfausty/32/5372_2.png) [@jfausty](https://discourse.vtk.org/u/jfausty)\
**Post date:** [April 24, 2023, 7:16am UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/5 "2023-04-24T07:16:07Z")

</div>

@LucasGandel might be interested by this

---

<div class="post-metadata">

**Author:** ![LucasGandel](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lucasgandel/32/1831_2.png) [@LucasGandel](https://discourse.vtk.org/u/LucasGandel)\
**Post date:** [April 24, 2023, 8:02am UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/6 "2023-04-24T08:02:11Z")

</div>

Similar issues are sometimes caused by the GC deleting VTK objects while the unmanaged code still references the collected object.  
I’ve only experienced this when using event handlers, but maybe vtkGlyph3D has similar issues.  
If you confirm that `glyph.SetSourceConnection(graphObj);` triggers the exception, can you try storing both `glyph` and `graphObj` as members in your class?  
This should increase the lifetime of those objects and prevent the GC from collecting them.

Note: The fact that the issues appears after some time probably confirms that the GC is involved.

---

<div class="post-metadata">

**Author:** ![LucasGandel](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lucasgandel/32/1831_2.png) [@LucasGandel](https://discourse.vtk.org/u/LucasGandel)\
**Post date:** [April 24, 2023, 8:02am UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/7 "2023-04-24T08:02:57Z")

</div>

I missed this one, please tag me for C# related questions. Thanks

---

<div class="post-metadata">

**Author:** ![Micromegas](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/ce73a5/32.png) [@Micromegas](https://discourse.vtk.org/u/Micromegas)\
**Post date:** [April 24, 2023, 8:12am UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/8 "2023-04-24T08:12:34Z")

</div>

For now, we just catch the bug and at least we don’t have any obvious problems as a result (In the long run, maybe we do) . I’ll try your approach and then report back if catching the object does anything. (But I think I have already tried this approach).  
My guess is that the garbage collector is throwing away something that is actually still needed.  
The reference count doesn’t seem to be really deterministic here either, although I would expect that.

---

<div class="post-metadata">

**Author:** ![LucasGandel](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lucasgandel/32/1831_2.png) [@LucasGandel](https://discourse.vtk.org/u/LucasGandel)\
**Post date:** [April 24, 2023, 8:20am UTC](https://discourse.vtk.org/t/system-accessviolationexception/11059/9 "2023-04-24T08:20:03Z")

</div>

> My guess is that the garbage collector is throwing away something that is actually still needed.

I agree. Identifying that object would help us fixing the problem in Activiz. Storing it in a class is a valid workaround in the meantime.  
I’ll try to reproduce in a small example and will update here if I find anything.
