Closed wwwMADwww closed 10 months ago
I googled and tinker around a bit. Consider this code from MonitorCaptureConfiguration.cs
:
Lets see using dnSpy what it translated to after compilation:
This new ScreenCaptureCallbackWithContext(MonitorCaptureConfiguration.OnNewFrame)
is successfully passed to unmanaged library but it's, as i assumed in OP, indeed used nowhere else and held by no one in managed code and thus can be GC'ed while some unmanaged code still use it. So this object needs to be placed in field or something, that's what i did in PR #157
Hello. I'm using library in .NET windows application through provided in
src_csharp
.NET wrappers.On tag 17.1.429 everything was fine. Today i tried pre-release 17.1.1327 and started getting random unrecoverable crashes. I looked in the windows Event Logs, sometimes it says:
It seems that when we setting callback for events like OnNewFrame, etc., the delegate is passed to the unmanaged code but for some reason nothing is holding it in the managed code, so it's get garbage collected on next GC run and no longer can be used by unmanaged code too.
How to reproduce:
Example_CSharp
and just wait. To speed things up better addGC.Collect();
in the end ofSCL_CreateMonitorCaptureManager
method beforereturn
statement.Result:
GC.Collect();
then application crashes immediately (on the next event i presume), with messages likeAn unhandled exception of type 'System.ExecutionEngineException' occurred in Unknown Module.
orFatal error. Internal CLR error. (0x80131506)