Marshal.copy 崩溃问题

我取了多张tiff图像,读到内存中为int[] 数组,而后通过线程池控制每张图像 尽心翻转操作。翻转使用的opencv,目前有一定的概率,在执行Marshal.Copy的时候,崩溃,使用try catch完全捕获不到,进程直接退出了。代码如下:

public static bool ImageMirrorCv(ref int[] image, int width, int height, int mirrorNum)
		{
			try
			{
				//以X轴,垂直翻转
				if (mirrorNum == 0)
				{
					Mat mt = new Mat(height, width, MatType.CV_32F);
					if ((mt.Height * mt.Width) != image.Length)
						return false;
					Marshal.Copy(image, 0, mt.Data, image.Count());
					Cv2.Flip(mt, mt, FlipMode.X);
					if ((mt.Height * mt.Width) != image.Length)
						return false;
					Marshal.Copy(mt.Data, image, 0, image.Count());
				}
				//以Y轴,水平翻转
				else if (mirrorNum == 1)
				{
					Mat mt = new Mat(height, width, MatType.CV_32F);
					if ((mt.Height * mt.Width) != image.Length)
						return false;
					Marshal.Copy(image, 0, mt.Data, image.Count());
					Cv2.Flip(mt, mt, FlipMode.Y);
					if ((mt.Height * mt.Width) != image.Length)
						return false;
					Marshal.Copy(mt.Data, image, 0, image.Count());
				}
				//XY翻转
				else if (mirrorNum == -1)
				{
					Mat mt = new Mat(height, width, MatType.CV_32F);
					if ((mt.Height * mt.Width) != image.Length)
						return false;
					Marshal.Copy(image, 0, mt.Data, image.Count());
					Cv2.Flip(mt, mt, FlipMode.XY);
					if ((mt.Height * mt.Width) != image.Length)
						return false;
					Marshal.Copy(mt.Data, image, 0, image.Count());
				}
				else
				{

				}
				return true;
			}
			catch(Exception ex)
			{
				Utility.LogControl.Error(ex.ToString());
				return false;
			}
			
		}

其中,输入输出,均为image,其为2048*2048的数据,height和width均为2048,mirrorNum控制翻转方向

报错后,系统事件查看器中显示:

目前使用频率大概在一天执行几万次,报错频率大概在一个月一次。

望大神不吝赐教

从异常信息来看很明显是在拷贝数据的时候访问了不可访问的内存造成的,try catch只能捕获托管代码的异常,捕获不到这个异常是因为造成异常的模块“VCRUNTIME140_CLR0400.dll”是一个非托管的dll,你可以理解为你调用了Marsh.Copy方法,实际上是由CLR去间接通过一系列其他方法调用了这个非托管dll的方法实现的。从你的代码来看基本上可以断定是读写mt.Data这个指针指向的数据的时候访问了不可访问的地址。