System.AccessViolationException

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?

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

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 Support

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 and https://github.com/stereolabs/zed-c-api

Stereolabs Support

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?