Hello folks!

I've faced a strange problem these days. I'm just on the way to finish a VideoGrabber class library in the NET 2013. This library is basically using a wrapper library for DirectShow (named AForge found on Sourceforge.net).
The VideoGrabber library is referenced and used by software written in the old fashionated Visual Basic 6. All objects and types are exposed as COM visible objects via type library. This was the only way how to connect VB6 application with NET code.

The VideoGrabber library has a simple video class which will initialize the DirectShow, select the source (it is a HD camera attached on USB 3.0) and start video grabbing. The video is read from the stream frame by frame and rendered in the NET PictureBox. The form containing this PictureBox is a part of a class library.

When a VB6 application makes an object instance, the video will be initialized and the video form will be displayed. After calling the method StartVideo, video stream will be rendered on the NET video form's PictureBox.
The problem is the following: after some time the video grabber will stop to work, a standard exception message box is shown with call stack and inside picturebox is a standard error image (white background with diagonal red cross).

This is happening only with this VB6 application but not with the NET demo application! Sometimet the VB6 application will work 5 seconds till error, but sometimes 1 minute. The result is always the same: exception was raised.

The another interesting thing is that the exception is always different! Sometimes "SEHException (0x80004005)", sometimes "System.OutOfMemoryException", sometimes "System.InvalidOperationException", or "System.ArgumentException".
Looks like a not released DC or some other handle. I took care and I cannot find what I did wrong.

Here is a sample exception message (it is non-sense to insert all of them):


Code:
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidOperationException: Object is currently in use elsewhere.
   at System.Drawing.Image.get_Width()
   at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1022 (RTMGDR.030319-1000)
    CodeBase: file:///c:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
DmsVideoSubsystem
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Projects/DmsVideoSubsystem.DLL
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1001 built by: RTMGDR
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.225 built by: RTMGDR
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1001 built by: RTMGDR
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
AForge.Video.DirectShow
    Assembly Version: 2.2.5.0
    Win32 Version: 2.2.5.0
    CodeBase: file:///C:/Projects/AForge.Video.DirectShow.DLL
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1002 built by: RTMGDR
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
AForge.Video
    Assembly Version: 2.2.5.0
    Win32 Version: 2.2.5.0
    CodeBase: file:///C:/Projects/AForge.Video.DLL
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1015 built by: RTMGDR
    CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

What am I doing wrong? Any idea?

Here is the sample code of the grabbing process itself:


Code:
Private Sub videoSource_NewFrame(ByVal sender As Object, ByVal eventArgs As AForge.Video.NewFrameEventArgs)
      bmp_frame = DirectCast(eventArgs.Frame.Clone(), Bitmap)
      eventArgs.Frame.Dispose()

      'Show video on internal NET form: 
      '---------------------------
      Try
            If bmp_frame IsNot Nothing Then
                  f.picVideo.Image = bmp_frame
            End If

            _Status = StatusEnum.VideoIsRunning

      Catch ex As Exception
            _Status = StatusEnum.ErrorOccured
      End Try
End Sub