System.AccessViolationException 复制结构时

System.AccessViolationException when copying a struct

我在 C++/CLI 项目中有这个 C++ 函数(非托管代码)。

    BMP Image;
    Image.ReadFromFile(filePath);
    HsvColor hsvPixel;
    RGBApixel startPixel;
    for (int i = 0; i < Image.TellWidth(); i++) {
        for (int j = 0; j < Image.TellHeight(); j++) {


            startPixel = *(Image(i, j));
            hsvPixel = RgbToHsv(startPixel);


            RGBApixel finalPixel = HsvToRgb(hsvPixel);
            *(Image(i, j)) = finalPixel;

        }
    }

(HsvColor 和 RGBApixel 都是无符号字符结构。Image(i, j) return 是指向 RGBApixel 的指针。)

有问题的行是 hsvPixel = RgbToHsv(startPixel);

这条线有两个问题:

  1. 它有时会导致 System.AccessViolationException,但并非总是如此。错误消息还说它试图写入受保护的内存。当我用调试器逐步执行代码时,RgbToHsv 函数 return 是一个值,然后它转到一些我看不到的系统函数,然后在返回到此处发布的代码块之前崩溃.

  2. 它没有用正确的值初始化 hsvPixel。我在调试模式下跟踪它,它 return 一个具有正确值的 HsvColor 对象,但是在执行该行之后,HsvColor 结构的无符号字符设置为与 returned 完全不同的值通过函数。

非常感谢您的帮助。

我也试过只执行RgbToHsv方法而不做任何赋值。当我这样做时,循环 运行 在大多数情况下都很好,但有时也会因 System.AccessViolationException 而崩溃,只是这种情况比对 hsvPixel 进行分配时要少得多。

RGBAPIxel 代码:

typedef struct RGBApixel {
    ebmpBYTE Blue;
    ebmpBYTE Green;
    ebmpBYTE Red;
    ebmpBYTE Alpha;
} RGBApixel; 

ebmpBYTE 是类型定义的无符号字符。

两种方法的代码:

RGBApixel HsvToRgb(HsvColor hsv)
{
    RGBApixel rgb;
    unsigned char region, remainder, p, q, t;

    if (hsv.Saturation == 0)
    {
        rgb.Red = hsv.Value;
        rgb.Green = hsv.Value;
        rgb.Blue = hsv.Value;
        rgb.Alpha = 1;
        return rgb;
    }

    region = hsv.Hue / 43;
    remainder = (hsv.Hue - (region * 43)) * 6;

    p = (hsv.Value * (255 - hsv.Saturation)) >> 8;
    q = (hsv.Value * (255 - ((hsv.Saturation * remainder) >> 8))) >> 8;
    t = (hsv.Value * (255 - ((hsv.Saturation * (255 - remainder)) >> 8))) >> 8;

    switch (region)
    {
    case 0:
        rgb.Red = hsv.Value; rgb.Green = t; rgb.Blue = p;
        break;
    case 1:
        rgb.Red = q; rgb.Green = hsv.Value; rgb.Blue = p;
        break;
    case 2:
        rgb.Red = p; rgb.Green = hsv.Value; rgb.Blue = t;
        break;
    case 3:
        rgb.Red = p; rgb.Green = q; rgb.Blue = hsv.Value;
        break;
    case 4:
        rgb.Red = t; rgb.Green = p; rgb.Blue = hsv.Value;
        break;
    default:
        rgb.Red = hsv.Value; rgb.Green = p; rgb.Blue = q;
        break;
    }

    return rgb;
}

HsvColor RgbToHsv(RGBApixel rgb)
{
    HsvColor hsv;
    unsigned char rgbMin, rgbMax;

    rgbMin = rgb.Red < rgb.Green ? (rgb.Red < rgb.Blue ? rgb.Red : rgb.Blue) : (rgb.Green < rgb.Blue ? rgb.Green : rgb.Blue);
    rgbMax = rgb.Red > rgb.Green ? (rgb.Red > rgb.Blue ? rgb.Red : rgb.Blue) : (rgb.Green > rgb.Blue ? rgb.Green : rgb.Blue);

    hsv.Value = rgbMax;
    if (hsv.Value == 0)
    {
        hsv.Hue = 0;
        hsv.Saturation = 0;
        return hsv;
    }

    hsv.Saturation = 255 * long(rgbMax - rgbMin) / hsv.Value;
    if (hsv.Saturation == 0)
    {
        hsv.Hue = 0;
        return hsv;
    }

    if (rgbMax == rgb.Red)
        hsv.Hue = 0 + 43 * (rgb.Green - rgb.Blue) / (rgbMax - rgbMin);
    else if (rgbMax == rgb.Green)
        hsv.Hue = 85 + 43 * (rgb.Blue - rgb.Red) / (rgbMax - rgbMin);
    else
        hsv.Hue = 171 + 43 * (rgb.Red - rgb.Green) / (rgbMax - rgbMin);

    return hsv;
}

Image(i, j) 的实现:

RGBApixel* BMP::operator()(int i, int j)
{
 using namespace std;
 bool Warn = false;
 if( i >= Width )
 { i = Width-1; Warn = true; }
 if( i < 0 )
 { i = 0; Warn = true; }
 if( j >= Height )
 { j = Height-1; Warn = true; }
 if( j < 0 )
 { j = 0; Warn = true; }
 if( Warn && EasyBMPwarnings )
 {
  cout << "EasyBMP Warning: Attempted to access non-existent pixel;" << endl
       << "                 Truncating request to fit in the range [0,"
       << Width-1 << "] x [0," << Height-1 << "]." << endl;
 }  
 return &(Pixels[i][j]);
}

Pixels的声明(来自EasyBMP库):

RGBApixel** Pixels;

编辑:我使用调试器查看 RgbToHsv 函数的执行方式。 startPixel 在第一次循环 运行s 时始终相同(应该如此)。但是当我点击Visual Studio中的"step into",看代码在函数中如何执行时,参数("rgb")总是和startPixel完全不同!我是调试新手,所以我可能会错误地解释事情。但是现在我更加困惑了。 Here is a picture.

我还应该提到代码的结果 运行。它输出图像,但输出图片只是随机单色(例如全蓝色),而它应该与输入图片相同。

确切的问题是@HansPassant 在他的评论中描述的问题:

You are likely battling a calling convention mismatch. The default for code compiled by the C++/CLI compiler is __clrcall, that is redrum on C functions that were built with __cdecl. You have to let the compiler know that the other code was not built the same way. We can't see the #includes you use, consider putting #pragma managed(push, off) before them and #pragma managed(pop) after them.

我为解决问题所做的是删除所有#pragma unmanaged/#pragma managed 行,而是将所有应以本机代码编译的 cpp 文件设置为不支持他描述的 clr here