sharpdx / SharpDX

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

Access Violation on Finalizer Thread #959

Open jkoritzinsky opened 6 years ago

jkoritzinsky commented 6 years ago

I've found an AV in https://github.com/AvaloniaUI/Avalonia/issues/1320 that's caused by SharpDX. It's happening in SharpDX.WIC.BitmapDecoder, so it's not in Avalonia's control. Here's what's happening:

General object layout:

SharpDX ComObject (wraps) Native COM object (has reference to) Native Thunk (for) ICallbackable object.

If the ICallbackable native shadow thunk(s) are finalized before the ComObject is finalized, then we get an access violation when the ComObject is finalized and the Native COM object tries to call Release on the native shadow thunk for the ICallbackable object.

Suggested Fix: Only de-allocate native shadow thunks once the refcount hits 0 instead of when dispose is called.

amerkoleci commented 6 years ago

Would you be able to submit a PR?

jkoritzinsky commented 6 years ago

I'm going to try to make a repro test case to verify that this is exactly what's happening and then submit a PR to fix it.