AccessViolationException in DeviceImage

Hi everyone,

I am currently working with the new Managed Wrappers, that will be part of the next CVB release.
I’m running into an exception that I’m not sure where to start searching for.


private TaskCompletionSource<int> _stopGrabTCS;
    
public async Task ProcessImages()
{
  var kernel = new double[3, 3]
    {
    { 0, -1, 0 },
    { -1, 4, -1 },
    { 0, -1, 0 },
    };
  Grabbing = false;
  await _stopGrabTCS.Task; // Is set to 1 in Grabbing, when the Grab was actually stopped.
  _device.RingBuffer.LockMode = RingBufferLockMode.On; 
  await SetTriggerMode(TriggerState.On);
  var stream = _device.Streams[0];

  while (State == States.Displaying)
  {
    using (CvbImage composedImage = new CvbImage(Image.Width, Image.Height))
    {
      Task[] processing = new Task[256];

      for (int i = 0; i < 256; i++)
      {
        SendSoftwareTrigger();
        var image = await stream.WaitAsync();
        NotifyOfPropertyChange(() => Image);

        processing[i] = Task.Run(() =>
        {
          using (var filterImage = Filter.User(image, kernel))
          {
           // Some more filter operations going on here,
          }
                {
                  CopyPixelsWithValue(filterImage, composedImage, (byte)i); // Writes values using LinearAccess
                }
              }
            }
          }
        });
      }
      await Task.WhenAll(processing);
      
      // This is where the exception occurs.
      //This call is needed, to free the RingBuffer, otherwise no more images
      // will be acquried when the buffer is full. In this case NumBuffers is set to 256.
      foreach (var img in _device.RingBuffer)  
      {
        img.Unlock();
      }
      // processing finished
     // Image gets saved to given path
    }
  }
 // Set camera back to freerun
}

Exception:
at Stemmer.Cvb.Runtime.InteropServices.Processing.GenericInvocation[TOut](StandardProcessingFunction1`1 fn)
at Stemmer.Cvb.Driver.RingBuffer.get_Count()
at Stemmer.Cvb.Driver.RingBuffer.d__22.MoveNext()

I had a short talk with @parsd, who told me, that this happens due to an error in RBNumbuffer.
When I had a look into the DeviceImage of the GeniCam.vin, I saw, that an AccessViolation occured.

The problem occurs randomly, sometimes the application runs a day without failing, sometimes it happens within the first 5 minutes.

I’m really puzzled and appreciate any help I can get.

What is the type of the exception thrown? According to your call stack it does get thrown near the call to the CVCDriver.dll function RBNumBuffer, but the actual nature and content might give more insight into the nature of the error that seems to occur on the unmanaged level.

The type of the exception is an ArgumentException.
I hope this is enough information for now, the application is running again, so I will have to wait for it to crash again, if further details are needed.

I just checked the GenICam.vin’s source code: CVC_E_PARAMETER only occurs in RBNumBuffer when the Device.Handle is IntPtr.Zero. So I assume that you have a life-time issue in your app. The code here only shows the symptom, but not the cause.

Bevore I forget to post the (temporary) final fix to my problem:
The trick was to call Dispose() on the image, after I created the filterImage.
When disposing the image right at the moment it is not needed anymore, everything works great.
When foreach-ing, then the above described error occurs.