# System.AccessViolationException

**URL:** <https://community.stereolabs.com/t/system-accessviolationexception/8564>\
**Category:** C#\
**Tags:** windows\
**Created:** [April 9, 2025, 9:37am UTC](https://community.stereolabs.com/t/system-accessviolationexception/8564 "2025-04-09T09:37:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangle1987](https://community.stereolabs.com/user_avatar/community.stereolabs.com/zhangle1987/32/4219_2.png) [@zhangle1987](https://community.stereolabs.com/u/zhangle1987)\
**Post date:** [April 9, 2025, 9:37am UTC](https://community.stereolabs.com/t/system-accessviolationexception/8564/1 "2025-04-09T09:37:15Z")

</div>

I implemented the simplest screen capture functionality in C#, but I encountered a `System.AccessViolationException` error.

System: Windows 11  
zed sdk:5.0  
cuda 12.8  
.net: .net 8.0

Can you help me?

```auto
 static void Main()
 {
     RuntimeParameters ZedRuntimeParams = new RuntimeParameters();
     Camera zed = new Camera(0);

     InitParameters initParams = new InitParameters();
     initParams.resolution = RESOLUTION.HD720; 
     initParams.cameraFPS = 30; 
     ERROR_CODE err = zed.Open(ref initParams);
     if (err != ERROR_CODE.SUCCESS)
     {
         Console.WriteLine("open fail: " + err);
         return;
     }

     
     sl.Mat image = new sl.Mat();
     image.Create((uint)zed.ImageWidth, (uint)zed.ImageHeight, MAT_TYPE.MAT_8U_C4, MEM.CPU);
     
     while (true)
     {
         if (zed.Grab(ref ZedRuntimeParams) == ERROR_CODE.SUCCESS)
         {
             
             zed.RetrieveImage(image, VIEW.LEFT); // here: System.AccessViolationException

             Console.WriteLine($"RetrieveImage success");
         }
         else
         {
             Console.WriteLine("RetrieveImage fail");
         }
     }

     // 关闭相机
     zed.Close();
 }

```

## error: Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. Repeat 2 times:

## at sl.Camera.dllz\_retrieve\_image(Int32, IntPtr, Int32, Int32, Int32, Int32, Int32)

at sl.Camera.RetrieveImage(sl.Mat, sl.VIEW, sl.MEM, sl.Resolution)  
at test6.Program.Main()

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [April 9, 2025, 10:07am UTC](https://community.stereolabs.com/t/system-accessviolationexception/8564/2 "2025-04-09T10:07:58Z")

</div>

I was able to reproduce the issue but it seems to be specific to .net8.0, I’m not sure why yet.

I’ll investigate on my side and push a fix once it’s ready.

Thanks for the report.

Best,

[Stereolabs](https://www.stereolabs.com/) Support

---

<div class="post-metadata">

**Author:** ![BenjaminV](https://community.stereolabs.com/user_avatar/community.stereolabs.com/benjaminv/32/3863_2.png) [@BenjaminV](https://community.stereolabs.com/u/BenjaminV)\
**Post date:** [April 17, 2025, 9:34am UTC](https://community.stereolabs.com/t/system-accessviolationexception/8564/3 "2025-04-17T09:34:02Z")

</div>

Hi,

I just found the source of the issue.  
I updated both the c and c-sharp wrappers on their respective repo.  
However, the nuget package will not be updated right now.

I’ll suggest using the latest version of the wrapper here [https://github.com/stereolabs/zed-csharp-api](https://github.com/stereolabs/zed-csharp-api) and [https://github.com/stereolabs/zed-c-api](https://github.com/stereolabs/zed-c-api)

[Stereolabs](https://www.stereolabs.com/) Support

---

<div class="post-metadata">

**Author:** ![digitalwannabe](https://community.stereolabs.com/user_avatar/community.stereolabs.com/digitalwannabe/32/4279_2.png) [@digitalwannabe](https://community.stereolabs.com/u/digitalwannabe)\
**Post date:** [April 17, 2025, 11:13am UTC](https://community.stereolabs.com/t/system-accessviolationexception/8564/4 "2025-04-17T11:13:47Z")

</div>

I’m running into the same problem here. Is there a solution to this in sight? Also, where will you push the fix to, the sdk5 is not public on github yet, is it?
