obiwanjacobi / vst.net

Virtual Studio Technology (VST) for .NET. Plugins and Host applications.
https://obiwanjacobi.github.io/vst.net/index.html
GNU Lesser General Public License v2.1
419 stars 51 forks source link

unhandled exception from Jacobi.Vst.Host.Interop on ProcessReplacing #56

Open cuikp opened 2 years ago

cuikp commented 2 years ago

When using Jacobi.Vst.Host.Interop from a .NET 5 host to interact with Effect-type VSTs (VST2), in some rare cases (only specific VSTs) calling ProcessReplacing crashes the managed host with an unhandled exception that is impossible to capture and handle from managed code (even the AppDomain.CurrentDomain.UnhandledException handler doesn't grab it) :

"An unhandled exception of type 'System.AccessViolationException' occurred in Jacobi.Vst.Host.Interop.dll Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Or, with native code debugging on in Visual Studio:

"Exception thrown at 0x00007FFD0603993D (SynthMaster2FX.dll) in QuikDawEditor.exe: 0xC0000005: Access violation reading location 0x0000000000000054"

To Reproduce Browsing the code I notice that ProcessReplacing is as follows:

float ppInputs = inputs->Length == 0 ? _emptyAudio32 : _audioInputs.GetArray(inputs->Length); float ppOutputs = outputs->Length == 0 ? _emptyAudio32 : _audioOutputs.GetArray(outputs->Length); int32_t inputSampleCount = CopyBufferPointers(ppInputs, inputs); int32_t outputSampleCount = CopyBufferPointers(ppOutputs, outputs); CallProcess32(ppInputs, ppOutputs, max(inputSampleCount, outputSampleCount));

(that is, without any error handling)

With all the numerous VSTs out there it would seem to be more trouble than it's worth to ferret out the specific cause of each error that a VST might be raising, but if the Vst.Host.Interop dll could simply handle any exceptions that are raised in this method (i.e not crash), it would allow the managed host program to grab the name of the VST so that it could just be ignored in the future.

(In case it's useful to mention, the name of one VST that is raising the error is the free plugin "SynthMaster2FX.dll" by KV331 Audio)

cuikp commented 2 years ago

By "impossible to capture and handle", I mean that the exception is not handleable by try/catch clauses, the WPF DispatcherUnhandledException handler or the AppDomain.CurrentDomain.UnhandledException handler, nor even by the AppDomain.CurrentDomain.FirstChanceException handler.