Page 2 of 3
Re: PictureBox Crashes
Posted: Wed Jul 06, 2016 10:46 pm
by sgraves
Juha,
I have found your code using _locker. I think there is a problem with it. The example you reference shows two tasks running and the processing is done inside locked regions using the _locker lock. The problem with this is that we don't have access to the second task. The paint operation is done in a task that is part of the VideoCaptureDevice class and that is our second task. I think that the answer is to lock the ImageBox inself. In the two places that you use
lock (_locker)
I have replaced _locker and have
lock (ImageBox)
So far I have not had the crash, but time will tell. It is like science. You can't prove it is true, you can only prove that it is false. But with enough time, I will begin to believe that it is fixed and that this is the "true" solution.
Steve
Re: PictureBox Crashes
Posted: Thu Jul 07, 2016 3:12 am
by Danal
Sounds like this may be fixed... but here's another data point that I did not realize until tonight: Even after a "Red X", if you hit continue, everything still works. The machine can find holes, place, etc. You just can't see it on the human display.
Danal
Re: PictureBox Crashes
Posted: Thu Jul 07, 2016 1:00 pm
by JuKu
> lock (_locker) vs lock (ImageBox)
You are probably right! The simple lock might leak, locking the reference but not the actual object. This would be a MS bug. It also kind of explains why some systems show the error, some don't (bug fixed in some release of the .NET, for example).
> after a "Red X", if you hit continue, everything still works
As expected. The software uses the camera processing for operations, it is the UI that crashes.
Re: PictureBox Crashes
Posted: Thu Jul 07, 2016 3:40 pm
by sgraves
Bad news. We are still crashing. It took a while, but then a crash and very quick crashes after the next three program starts.
My background is embedded systems, Windows programming is not in my wheel house. It seems to me that we don't have a way to tell the task that runs paint that we are manipulating a shared resource. If I had the equivalent situation in an embedded system, I would disable interrupts. There must be a way to prevent task switching while we are accessing the shared resource. Windows programmers, how about it. Is there a way?
Re: PictureBox Crashes
Posted: Thu Jul 07, 2016 6:47 pm
by sgraves
Ok, I am trying something different. I am back to -locker. I have created a new class and overrode the OnPaint method.
Code: Select all
private static object _locker = new object();
// Image= PictureBox in UI, the final shown image
// Frame= picture from camera
public class ProtectedPictureBox : System.Windows.Forms.PictureBox
{
protected override void OnPaint(PaintEventArgs e)
{
lock (_locker)
{
base.OnPaint(e);
}
}
}
I used the lock where Juha had it before and I added it here as well.
Code: Select all
private void Video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
ReceivingFrames = true;
lock (_locker)
{
frame = (Bitmap)eventArgs.Frame.Clone();
}
We are running with these changes. As before, only time will tell if we have it fixed.
There are more changes than I show above. Once I am confident the problem is fixed and I figure out Git, I will create a pull request.
Re: PictureBox Crashes
Posted: Fri Jul 08, 2016 8:21 pm
by JuKu
sgraves wrote:We have been getting crashes which say "InvalidOperationException - object is currently in use elsewhere" for mainly the Placement_PictureBox and it goes to a box with red diagonal lines. We must restart the program to get the image back.
At line 6077 in MainForm.cs we have the following
Code: Select all
private void Placement_pictureBox_MouseMove(object sender, MouseEventArgs e)
{
General_pictureBox_MouseMove(Tapes_pictureBox, e.X, e.Y);
}
Is this a bug? Or is the Tapes_pictureBox object supposed to be passed along in this function?
It should pass on reference to the Placement_pictureBox, of course. A copy-paste error that doesn't matter, as the boxes are aligned anyway. I fixed it anyway, thank you for pointing that out!
Re: PictureBox Crashes
Posted: Fri Jul 08, 2016 8:26 pm
by JuKu
I've done some learning on this also. Finding out that the paint method caused the error and reading about it, I'm confident that overriding the event needs to be done. It might even be
the solution (one can always hope), but at least, it is a part of it. I'm taking two weeks off with my wife starting tomorrow, but I think this is important enough to make a release, and not wait until I'm back.
Release:
http://www.liteplacer.com/Downloads/Lit ... 7_2016.exe
Re: PictureBox Crashes
Posted: Fri Jul 08, 2016 11:35 pm
by sgraves
Juha,
Good news, we have run all day without a single crash. We would have had multiple crashes in the same time period before the change. Evidence is strong that this is the fix.
I had been trying to figure out what I have been doing in git. Finally after understanding that the files change in the working directory to the checked branch I was able to isolate these changes to one branch. I hadn't read this post and so you will see a pull request with my changes.
Thanks,
Steve
P.S. Enjoy your vacation, I hope it is relaxing and rejuvenating. You deserve it!
Re: PictureBox Crashes
Posted: Mon Jul 11, 2016 7:11 pm
by sgraves
Still no crashes due to sharing violations, but we have had a new one. Tracking this one down...
It results in the same red cross.
Code: Select all
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.ArgumentException: Parameter is not valid.
at System.Drawing.Image.get_Width()
at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at LitePlacer.Camera.ProtectedPictureBox.OnPaint(PaintEventArgs e)
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.6.1080.0 built by: NETFXREL3STAGE
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
LitePlacer
Assembly Version: 1.0.6035.40727
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/test/Desktop/Debug/LP_w_data/LitePlacer.exe
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1038.0 built by: NETFXREL2
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1081.0 built by: NETFXREL3STAGE
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1038.0 built by: NETFXREL2
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1078.0 built by: NETFXREL3STAGE
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1038.0 built by: NETFXREL2
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1064.2 built by: NETFXREL3STAGE
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
AForge.Video.DirectShow
Assembly Version: 2.2.5.0
Win32 Version: 2.2.5.0
CodeBase: file:///C:/Users/test/Desktop/Debug/LP_w_data/AForge.Video.DirectShow.DLL
----------------------------------------
AForge.Video
Assembly Version: 2.2.5.0
Win32 Version: 2.2.5.0
CodeBase: file:///C:/Users/test/Desktop/Debug/LP_w_data/AForge.Video.DLL
----------------------------------------
AForge.Imaging
Assembly Version: 2.2.5.0
Win32 Version: 2.2.5.0
CodeBase: file:///C:/Users/test/Desktop/Debug/LP_w_data/AForge.Imaging.DLL
----------------------------------------
AForge
Assembly Version: 2.2.5.0
Win32 Version: 2.2.5.0
CodeBase: file:///C:/Users/test/Desktop/Debug/LP_w_data/AForge.DLL
----------------------------------------
System.Web.Extensions
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1073.0
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Web.Extensions/v4.0_4.0.0.0__31bf3856ad364e35/System.Web.Extensions.dll
----------------------------------------
System.Web
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1073.0 built by: NETFXREL3STAGE
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Web/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Web.dll
----------------------------------------
AForge.Math
Assembly Version: 2.2.5.0
Win32 Version: 2.2.5.0
CodeBase: file:///C:/Users/test/Desktop/Debug/LP_w_data/AForge.Math.DLL
----------------------------------------
HomographyEstimation
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/test/Desktop/Debug/LP_w_data/HomographyEstimation.DLL
----------------------------------------
MathNet.Numerics
Assembly Version: 3.5.0.0
Win32 Version: 3.5.0.0
CodeBase: file:///C:/Users/test/Desktop/Debug/LP_w_data/MathNet.Numerics.DLL
----------------------------------------
System.Numerics
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1038.0 built by: NETFXREL2
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
----------------------------------------
System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.6.1038.0 built by: NETFXREL2
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.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.
Re: PictureBox Crashes
Posted: Tue Jul 12, 2016 2:04 am
by sgraves
It seemed like the last crash was happening at tab switches. During tab switching the camera ImageBox is set to new PictureBoxes. I decided to add the ImageBox setter to the region protected by the _locker lock. It has been pushed to the PictureBoxCrashFix branch.
https://github.com/sgraves/LitePlacer-D ... oxCrashFix