# AccessViolationException in DeviceImage

**URL:** <https://forum.commonvisionblox.com/t/accessviolationexception-in-deviceimage/247>\
**Category:** .Net API\
**Tags:** csharp, acquisition, pointer\
**Created:** [October 9, 2017, 8:45am UTC](https://forum.commonvisionblox.com/t/accessviolationexception-in-deviceimage/247 "2017-10-09T08:45:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [October 9, 2017, 8:45am UTC](https://forum.commonvisionblox.com/t/accessviolationexception-in-deviceimage/247/1 "2017-10-09T08:45:30Z")

</div>

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.

```auto

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.

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [October 9, 2017, 9:07am UTC](https://forum.commonvisionblox.com/t/accessviolationexception-in-deviceimage/247/2 "2017-10-09T09:07:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [October 9, 2017, 9:14am UTC](https://forum.commonvisionblox.com/t/accessviolationexception-in-deviceimage/247/3 "2017-10-09T09:14:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [October 9, 2017, 11:24am UTC](https://forum.commonvisionblox.com/t/accessviolationexception-in-deviceimage/247/4 "2017-10-09T11:24:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chris](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/chris/32/1154_2.png) [@Chris](https://forum.commonvisionblox.com/u/Chris)\
**Post date:** [October 24, 2017, 9:14am UTC](https://forum.commonvisionblox.com/t/accessviolationexception-in-deviceimage/247/5 "2017-10-24T09:14:41Z")

</div>

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.
