Random crashes while retrieving image

Hello,

Sometimes I encounter with exceptions while working with Stemmer.Cvb library:

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at Stemmer.Cvb.DriverLib.RBLockMode(IntPtr, Int32, Int32 ByRef)
at Stemmer.Cvb.Runtime.InteropServices.Processing.GenericInvocation(StandardProcessingFunctionVoid, ReturnCodeTranslation)
at Stemmer.Cvb.Runtime.InteropServices.Processing.StandardProcessing(StandardProcessingFunctionVoid)
at Stemmer.Cvb.Driver.RingBuffer.LockModeFromHandle(IntPtr)
at Stemmer.Cvb.Driver.RingBufferImage.Dispose(Boolean)
at Stemmer.Cvb.Image.Dispose()
at Framework.Hardware.Camera.StemmerCVB.CvbCamera+d__62.MoveNext()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action, Boolean, System.Threading.Tasks.Task ByRef)
at System.Threading.Tasks.Task.FinishContinuations()
at System.Threading.Tasks.Task1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon) at System.Threading.Tasks.TaskCompletionSource1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon)
at Stemmer.Cvb.Async.AcquisitionExtensions+AsyncManager.Worker()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at Stemmer.Cvb.DriverLib.RBLockMode(IntPtr, Int32, Int32 ByRef)
at Stemmer.Cvb.Runtime.InteropServices.Processing.GenericInvocation(StandardProcessingFunctionVoid, ReturnCodeTranslation)
at Stemmer.Cvb.Runtime.InteropServices.Processing.StandardProcessing(StandardProcessingFunctionVoid) at Stemmer.Cvb.Driver.RingBuffer.LockModeFromHandle(IntPtr)
at Stemmer.Cvb.Driver.RingBufferImage.Dispose(Boolean) at Stemmer.Cvb.Image.Dispose()
at Famework.Hardware.Camera.StemmerCVB.CvbCamera+d__62.MoveNext()
at Famework.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action, Boolean, System.Threading.Tasks.Task ByRef) at System.Threading.Tasks.Task.FinishContinuations()
at System.Threading.Tasks.Task1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon)
at System.Threading.Tasks.TaskCompletionSource1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon)
at Stemmer.Cvb.Async.AcquisitionExtensions+AsyncManager.Worker()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
at Stemmer.Cvb.DriverLib.RBLockMode(IntPtr, Int32, Int32 ByRef)
at Stemmer.Cvb.Runtime.InteropServices.Processing.GenericInvocation(StandardProcessingFunctionVoid, ReturnCodeTranslation)
at Stemmer.Cvb.Runtime.InteropServices.Processing.StandardProcessing(StandardProcessingFunctionVoid)
at Stemmer.Cvb.Driver.RingBuffer.LockModeFromHandle(IntPtr)
at Stemmer.Cvb.Driver.RingBufferImage.Dispose(Boolean)
at Stemmer.Cvb.Image.Dispose()
at Famework.Hardware.Camera.StemmerCVB.CvbCamera+d__62.MoveNext()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run()
at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action, Boolean, System.Threading.Tasks.Task ByRef)
at System.Threading.Tasks.Task.FinishContinuations()
at System.Threading.Tasks.Task1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon) at System.Threading.Tasks.TaskCompletionSource1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].TrySetResult(System.__Canon)
at Stemmer.Cvb.Async.AcquisitionExtensions+AsyncManager.Worker()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()

async Task StartGrabAsync() 
{
while (IsGrabbing)
 {
  using (var currentImage = await stream.WaitAsync())
  {
   //...
  }
 }
}
//...
public override void StopGrab()
{
 if (!IsGrabbing) return;

 Camera.Stream.Stop();
}

Do you have any ideas why this could happen? This happens relatively rarely, mostly when executing StopGrab method.

Hi @EdvinasD ,

could you please provide us more information on your Grabbing property?
I assume you only toggle a boolean here and not actually return the stream.IsRunning property.
As you are working async it might very well happen, that you try to Wait for an image, while the camera was already stopped, but the IsGrabbing property was not refreshed yet.

Cheers
Chris

IsGrabbing property looks like this:

public override bool IsGrabbing => Camera != null && !Camera.IsDisposed && !DeviceNodeMap.IsDisposed && Camera.Stream.IsRunning;

Could you try and stript this property down to

IsGrabbing => Device.Stream.IsRunning;

I want to avoid any overhead here that could cause timing issues (which is very likely for a NodeMap access for example).