zuloloxi / aforge

Automatically exported from code.google.com/p/aforge
Other
0 stars 0 forks source link

Video.DirectShow exception in Dispose #274

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create console app
2. Add attached file WebCam2.cs
3. Call WebCam2 method TakeWebCamSnapShot(....) in main
4. Open another program (like skype) where turn on webcam (let it run)
5. Run console app (result is no snapshot it's OK) but when close app occure 
error:

ReportAvOnComRelease was detected
Message: An exception was caught but handled while releasing a COM interface 
pointer through Marshal.Release, Marshal.ReleaseComObject or implicitly after 
the corresponding RuntimeCallableWrapper was garbage collected. This is the 
result of a user refcount error or other problem with a COM object's Release. 
Make sure refcounts are managed properly. While these types of exceptions are 
caught by the CLR, they can still lead to corruption and data loss so if 
possible the issue causing the exception should be addressed

Expect is app close correctly or add method to detecting camera is not using 
another program or is available

Version: 2.2.3.0

Exception occure in:
Video.DirectShow->Internals->Structures.cs
[AForge.Video.DirectShow.Internals.AMMediaType]
method: protected virtual void Dispose( bool disposing )
line: 128 -> Marshal.Release( unkPtr );

sry for my english, I'm from Slovakia 

Original issue reported on code.google.com by mtester...@gmail.com on 3 Jan 2012 at 3:00

Attachments:

GoogleCodeExporter commented 8 years ago
When i build my project at Release occure another problem... windows throw 
windows "Windows is collecting more information about the problem."

When at Debug windows dont get any problem with my app 

WHY??

Original comment by mtester...@gmail.com on 13 Jan 2012 at 9:39

GoogleCodeExporter commented 8 years ago
Don't know anything about YOUR project. My projects work fine in Release.

Original comment by andrew.k...@gmail.com on 13 Jan 2012 at 9:44

GoogleCodeExporter commented 8 years ago
I figure it out:

In class AMMediaType I remove IDisposable interface and remove all method to 
dispose (comented all lines where call (AAMediaType).Dispose() method)

All work fine in Debug and Release mode, no exception was raised.

I tested only in case when Capture single image (snapshot) from webcam.

Original comment by mtester...@gmail.com on 17 Jan 2012 at 9:09

Attachments:

GoogleCodeExporter commented 8 years ago
Does not look like a smart fix to me. Dispose method takes care of freeing 
memory. So with the fix like yours you just create potential memory leaks.

Original comment by andrew.k...@gmail.com on 17 Jan 2012 at 9:42

GoogleCodeExporter commented 8 years ago
Yes you are right I think this too, but when Dispose be there program after 
closing failing, when not be there program work fine all resources was free out 
(I thing so), because camera can be used by another program when program 
running or not.

Original code failing on Releasing "unkPtr", this attribute dont use anywhere 
(this is write in comments: "Not used.") but pointer was set. Why call this 
Marshal.Release(unkPtr); ???

Original comment by mtester...@gmail.com on 17 Jan 2012 at 10:55

GoogleCodeExporter commented 8 years ago
1) The issue never happen in the case when there is NO another application 
using the camera. So if your application starts using a camera, it allocates 
resources, those need to be freed in the end. This sounds as a correct approach 
to me.

2) >> Why call this Marshal.Release(unkPtr);
Because it is not NULL. Instead of commenting this line of code, I would better 
investigate:
a) why is it not NULL. I would think that if application fails to get access to 
a camera, than some pointers should be set to NULL. And if some of them are not 
NULL, then it should be safe to call Release() for them.
b) how to detect that the camera is in use and then don't try doing anything 
else.

Original comment by andrew.k...@gmail.com on 17 Jan 2012 at 11:05

GoogleCodeExporter commented 8 years ago
Ok Adrew soo the conclusion is what?? (you try this cases? or you still dont 
have a camera?)

Dont use Aforge.NET when camera should be use by another program or what? I 
dont see any solution :)

Original comment by mtester...@gmail.com on 17 Jan 2012 at 12:33

GoogleCodeExporter commented 8 years ago
>> or you still dont have a camera?
time, which is more expensive.

>> Dont use Aforge.NET when camera should be use by another program or what? 
1) What do you expect to get when camera is in use by another application? You 
will get nothing anyway. We just need to fix the crash.
2) The code is open - you can fix the issue as well. As you already mentioned 
you have a working workaround. So use it in the meantime while there is no fix.

Original comment by andrew.k...@gmail.com on 17 Jan 2012 at 1:29

GoogleCodeExporter commented 8 years ago
I tried your code - works fine for me without any exceptions.

Which system do you have? 32 or 64 bit OS?

Original comment by andrew.k...@gmail.com on 18 Jan 2012 at 8:15

GoogleCodeExporter commented 8 years ago
I've 64bit win 7

Original comment by mtester...@gmail.com on 18 Jan 2012 at 10:06

GoogleCodeExporter commented 8 years ago
Two things to try:
1) Run you application in 32-bit mode - this should work fine.
or
2) Try using the attached file. Just overwrite this file in AForge.NET 
framework's sources with a new version and then rebuild 
AForge.Video.DirectShow.dll. Hopefully it will help. Sorry, cannot try myself 
now - don't have 64 bit Win next to me.

Original comment by andrew.k...@gmail.com on 19 Jan 2012 at 4:35

Attachments:

GoogleCodeExporter commented 8 years ago
I try it when i have a time and I send here a result. 

btw. i don't use DLL, I'm using .cs files from AForge

Original comment by mtester...@gmail.com on 19 Jan 2012 at 4:58

GoogleCodeExporter commented 8 years ago
>> btw. i don't use DLL, I'm using .cs files from AForge
It is up to you. Just make sure your application is open source then.

Original comment by andrew.k...@gmail.com on 19 Jan 2012 at 5:56

GoogleCodeExporter commented 8 years ago
Result on x64 machine is:

1) when replace Structure.cs (Platform target=Any CPU)program still crashing
2) when replace Structure.cs and change Platform target (project property) to 
x64 from Any CPU program still crashing
3) when replace Structure.cs and change Platform target to x86 program works!!!

4) keep original Structure.cs from AForge 2.2.3.0, Platform x64, program 
crashing
5) keep original Structure.cs from AForge 2.2.3.0, Platform x86, program 
works!!!

When is x64 machine or project platform set to x64 unkPtr set to 123462096 in 
my case :) it's crazy

Don't worry Adrew I dont sell my application :) it's only for fun

Original comment by mtester...@gmail.com on 20 Jan 2012 at 7:46

GoogleCodeExporter commented 8 years ago
Need win 64 for further debugging.

Original comment by andrew.k...@gmail.com on 20 Jan 2012 at 9:42

GoogleCodeExporter commented 8 years ago

Original comment by andrew.k...@gmail.com on 23 Jan 2012 at 11:46