1

I am rendering video frames on writable bitmap, and application crashing randomly after sometime, am monitoring memory and cpu load looks fine

Frames are grabbed and queued for rendering from threadpool thread and am invoking following code on dispatcher

    this.Dispatcher.Invoke((Action)(() =>
                {

    BitmapData dataRE = rData.Frame.Bitmap.LockBits(new Rectangle(0, 0, rData.Frame.Bitmap.Width, rData.Frame.Bitmap.Height), ImageLockMode.ReadWrite, rData.Frame.Bitmap.PixelFormat);
                        rightEyeWB.Lock();
                        rightEyeWB.WritePixels(new Int32Rect(0, 0, rData.Frame.Bitmap.Width, rData.Frame.Bitmap.Height), dataRE.Scan0, rData.Frame.Bitmap.Width * rData.Frame.Bitmap.Height, dataRE.Stride);
                        rightEyeWB.Unlock();

                }), DispatcherPriority.Send);
 rData.Frame.Dispose();

and when I check windows log for crash report I got following trace

   Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
  at System.Windows.Media.MILUtilities.MILCopyPixelBuffer(Byte*, UInt32, UInt32, UInt32, Byte*, UInt32, UInt32, UInt32, UInt32, UInt32)
  at System.Windows.Media.Imaging.WriteableBitmap.WritePixelsImpl(System.Windows.Int32Rect, IntPtr, Int32, Int32, Int32, Int32, Boolean)
  at System.Windows.Media.Imaging.WriteableBitmap.WritePixels(System.Windows.Int32Rect, IntPtr, Int32, Int32)
  at Cyclops.View.CameraWindow.renderPupilFrames()
  at Cyclops.View.CameraWindow.<grabFrames>b__11()
  at System.Windows.Threading.DispatcherOperation.InvokeDelegateCore()
  at System.Windows.Threading.DispatcherOperation.InvokeImpl()
  at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
  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 MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object)
  at System.Windows.Threading.DispatcherOperation.Invoke()
  at System.Windows.Threading.Dispatcher.ProcessQueue()
  at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
  at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
  at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
  at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
  at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
Dark Knight
  • 3,507
  • 8
  • 35
  • 44
  • 1
    Afaik, using Lock and Unlock isn't necessary for WritePixels. You would use them for locking access to the BackBuffer property, and you wouldn't do that in the UI thread. See the Remarks here: https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.writeablebitmap(v=vs.110).aspx#Remarks – Clemens Feb 21 '18 at 11:27
  • @Clemens I removed lock/unlock for writablebitmap, and still app crashes after rendering 2-3 mins. strange thing is its very random and doesn't happen every time – Dark Knight Feb 22 '18 at 10:16
  • Wouldn't it be a better idea anyway not to run this in the Dispatcher, but instead access the BackBuffer in the background thread? – Clemens Feb 22 '18 at 10:21
  • Ok I ll try that – Dark Knight Feb 23 '18 at 09:13

0 Answers0