Attempted to read or write protected memory on MWSafeHandle

I'm getting an error back when running my C# program that interfaces with the MCR (using MATLAB R2015b). The error happens very infrequently and is difficult to reproduce. When it occurs, it states the following:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
And the stack trace for the error is:
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at MathWorks.MATLAB.NET.Utility.MWSafeHandle.mclMxRefDestroyArray(IntPtr pMXArray)
at MathWorks.MATLAB.NET.Utility.MWSafeHandle.ReleaseHandle()
at System.Runtime.InteropServices.SafeHandle.InternalFinalize()
at System.Runtime.InteropServices.SafeHandle.Finalize()
Data of System.AccessViolationException: No entries.
I'm using MWArrays in my program, and the issue seems to be the finalizer tripping up on a MWSafeHandle.
I never explicitly use the MWSafeHandle, however I try and call the Dispose() method on each of my MWArrays when I'm done using them (and I'm assuming this is correlated to the MWSafeHandle finalizer issue).
Specifically invoking the Dispose() method doesn't really seem to be necessary (https://www.mathworks.com/help/dotnetbuilder/MWArrayAPI/html/57290a83-9e6e-4179-a1b8-9fb0344544ce.htm) but I want ot reduce the uncertainty and memory footprint of my program by disposing of the MWArrays myself.
Some additional observations I've made is that when I'm profiling my program using the .NET memory profiler, I'm seeing that the MWSafeHandle is marked as being garbage collected but not properly disposed. I viewed the disposed instances after running my program and specifically calling garbage collection using:
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
I attached a screenshot of the profiling results I'm getting after garbage collection.
Here is some more documentation on the error: https://memprofiler.com/online-docs/default.htm#!undisposedinstances.htm
Does deterministically calling the Dispose() method on MWArrays lead to problems? If I forgot to deterministically dispose of the MWArrays somewhere could that also lead to issues?
Any help is appreciated, and if more information is needed, I'd be happy to provide it!
Additionally, I've recieved the following advice on the issue - but it didn't help:
"A possible cause for this error can appear if you copied the MATLAB Runtime DLL "mclmcrrt711.dll" to the folder that contains your .NET application or to some other Windows system folder. If you copy this MATLAB DLL to another folder, when it tries to find its further dependencies it will fail."
Other suggestions say the issue can be caused by having mixed dependencies (some dependencies for the x64 platform and some for the x86 platform). But I've checked, and I don't have dependency issues.
Thanks!

Answers (0)

Products

Release

R2015b

Asked:

on 9 Aug 2019

Edited:

on 9 Aug 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!