sharpdx / SharpDX

SharpDX GitHub Repository
http://sharpdx.org
MIT License
1.7k stars 639 forks source link

How to find the cause of cast Thread-ComObjectShadow exception #1052

Open astrowalker opened 6 years ago

astrowalker commented 6 years ago

I am working on "player" app -- show a frame, play movie, show a frame. From time to time (when changing code) I encounter strange crashes

Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Threading.Thread' to type 'SharpDX.ComObjectShadow'.
   at SharpDX.CppObjectShadow.ToShadow[T](IntPtr thisPtr)
   at SharpDX.ComObjectShadow.ComObjectVtbl.AddRefImpl(IntPtr thisObject)

The first time the reason was I was using ConfigurationSection type and I was fetching data from it while playing movie.

The second time was when I added some code which took Func as parameter. When I refactored code and eliminated Func (instead I evaluate it locally and pass only the outcome of it) the crash vanished.

Now is the third time -- I added support for playing several movies. So I created a list of media engines, I preload all requested movies -- and in some cases, and on some computers I see this crash again. For example on one computer I can play 3 or 4 movies, but not 1 or 2.

My concern here is how I should debug my app to connect somehow this crash with (completely valid) my C# code? So far it was trial&error plus some guessing/luck, but this is not a recipe for success.

I am grateful for all tips and advices in advance how to make a "connection" between my code and this exception.

jkoritzinsky commented 6 years ago

My initial thought is that the native code you're interfacing with is corrupting the GCHandle value and by happenstance setting it to a value that corresponds to a Thread instance. If you use a native debugger, you could set a data breakpoint to watch the memory and see what's changing it.

astrowalker commented 6 years ago

@jkoritzinsky , thank you. Do you mean DirectX by "native code"? In sense that I use SharpDX and it wraps around native DirectX? Other than that (SharpDX->DirectX) I use pure C# code based on SharpDX examples with some added logic on my own (like counting time, reacting to keypresses, writing logs to a file).

jkoritzinsky commented 6 years ago

Yeah I'm referring to DirectX by "native code". Any chance you can upload a repro of this issue so I can fully investigate this?

astrowalker commented 6 years ago

@jkoritzinsky , thank you, unfortunately I cannot. Luckily I found the problem again using trial&error (I used MediaEngine for starting playback, but MediaEngineEx for fetching frames; weird it worked for single movie for ~ą year without a problem), but anyway my question still stands -- how to debug such error in systematic way?

jkoritzinsky commented 6 years ago

Without seeing any of the code that's causing the issue, all I can do is make guesses. Any chance you could get a stack trace with both the native and managed debugger attached? That might help me give you more information.

jkoritzinsky commented 6 years ago

@astrowalker any chance this is happening on the Finalizer thread? I'm having success reproducing this in another project on the finalizer thread.

astrowalker commented 6 years ago

@jkoritzinsky , currently my code works (luckily) so I cannot tell at the moment.