Open MillerJames opened 5 years ago
Hey can you open an issue for this on the SharpGenTools repo? That's where the code generator lives. I'm busy with work right now but I'd definitely be interested in adding this into the generator.
Sure, I just opened CppObject finalized before native method is called in SharpGenTools, basically just copy-pasted this issue. Thanks!
I’ve found that with Configuration.EnableReleaseOnFinalizer set to true, I occasionally get an exception if the last use of a ComObject is passing it into a generated interop method. For example, when using SharpDX versions 3.1.1 or 4.2.0, the following call to
d2drendertarget.FillGeometry
occasionally throws an exception. I believe this is because the finalizer runs and decrements the ref count to zero before the native method acquires its own ref:This case can be trivially resolved with a using block in my code, but it seems like the point of Configuration.EnableReleaseOnFinalizer is that explicitly disposing is not required. I'm working around this issue with using blocks and GC.KeepAlive for the cases I've found in my codebase, but resolving this more generally would be great.
Would it be reasonable to have SharpGen generate GC.KeepAlive calls to delay finalization for objects whose native pointer is passed into a native method call? i.e. the generated method would look something like: