У меня проблема с моим кодом, где он работает правильно,
но в случайное время вызов функции Marshal.Copy дает мне исключение нарушения прав доступа.
Моя программа получает изображение с рентгеновского сканера.
Рентгеновский сканер помещает изображение в память и дает мне указатель, откуда я получаю эту картинку.
Я использую Marsharl.Copy, чтобы поместить информацию в указателе в байтовый массив размером 2 * Ширина (# x пикселей) * Высота (# число Y пикселей) и обработать ее.
вот функция, которая вызывается каждые 1,5 с или около того, чтобы получить изображение
public void GetLiveImage()
{
VirtCp.SLivePrms lp = new VirtCp.SLivePrms();
Mode mode = Modes.Where(x => x.ModeNumber == CurrentMode).First<Mode>();
MemoryError = false;
OkProceed = false;
int errCode = 0;
int timeout = 0;
xPixelSize = (int)mode.ColumnsPerFrame;
yPixelSize = (int)mode.LinesPerFrame;
imageSize = 2 * xPixelSize * yPixelSize;
IntPtr image = Marshal.AllocHGlobal(imageSize);
logger.WriteLine("Calling function to obtain image");
errCode = VirtCp.VipFluoroGetPrms(ref lp); // this function from the scanner dll gives points me to the data of the image
while (errCode != (int)HCPError.HCP_NO_ERR)
{
timeout++;
if (timeout > 350000)
{
logger.WriteLine("There are problems with detector:" + HCPErrorText.GetErrorText(errCode));
OkProceed = false;
return;
}
errCode = VirtCp.VipFluoroGetPrms(ref lp);
}
byteImage = new byte[imageSize];
try
{
if (errCode == (int)HCPError.HCP_NO_ERR)
{
logger.WriteLine("Transfering image byte data for processing");
Marshal.Copy(lp.BufPtr, byteImage, 0, imageSize); // this is the copy function that works 80% of the time, but at random it throws me into a memory access violation
logger.WriteLine("Saving raw and bmp images");
bool firstrun = true;
int BW_timeout = 0;
while ((ImageProcess_1.IsBusy && ImageProcess_2.IsBusy && ImageProcess_3.IsBusy) || firstrun)
{
firstrun = false;
BW_timeout++;
if (BW_timeout >= 300000)
{
logger.WriteLine("Errors trying to convert images");
OkProceed = false;
return;
}
if (!ImageProcess_1.IsBusy)
{
ImageProcess_1.RunWorkerAsync();
}
else if (!ImageProcess_2.IsBusy)
{
ImageProcess_2.RunWorkerAsync();
}
else if (!ImageProcess_3.IsBusy)
{
ImageProcess_3.RunWorkerAsync();
}
}
OkProceed = true;
}
else
{
logger.WriteLine("Error from detector function call: " + HCPErrorText.GetErrorText(errCode));
OkProceed = false;
logger.WriteLine("Will Try to obtain image again");
}
}
catch (Exception ex)
{
logger.WriteLine(ex.Message);
MemoryError = true;
}
try
{
Marshal.FreeHGlobal(image);
}
catch (Exception ex)
{
logger.WriteLine(ex.Message);
}
}