gpt4 book ai didi

c++ - 复制结构时出现 System.AccessViolationException

转载 作者:行者123 更新时间:2023-11-30 05:08:52 25 4
gpt4 key购买 nike

我在 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) 返回指向 RGBApixel 的指针。)

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

这条线有两个问题:

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

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

如果能提供一些帮助,我将不胜感激。

我也尝试过只执行 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 在循环第一次运行时始终相同(应该如此)。但是,当我在 Visual Studio 中单击“进入”并查看代码在函数中的执行方式时,参数(“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 行,而是将所有应以 native 代码编译的 cpp 文件设置为不支持他描述的 clr here .

关于c++ - 复制结构时出现 System.AccessViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46549853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com