在混合代码中查找违反访问的起源



我得到了以下代码(使用sharpdx.wic/sharpdx.direct3d11),它总是抛出一个例外,说违反了访问。

public static SharpDX.Direct3D11.Texture2D CreateTexture2DFromBitmap(SharpDX.Direct3D11.Device device, BitmapSource bitmapSource)
{
    // Allocate DataStream to receive the WIC image pixels
    int stride = PixelFormat.GetStride(bitmapSource.PixelFormat, bitmapSource.Size.Width);
    using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true))
    {
        // Copy the content of the WIC to the buffer
        bitmapSource.CopyPixels(stride, buffer);
        return new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
        {
            Width = bitmapSource.Size.Width,
            Height = bitmapSource.Size.Height,
            ArraySize = 1,
            BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
            Usage = SharpDX.Direct3D11.ResourceUsage.Default,
            CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
            Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
            MipLevels = 1,
            OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
            SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
        }, new DataRectangle(buffer.DataPointer , stride));
    }
}

例外消息是:

在myApp.exe中丢弃在0x00007ff894eca25c(bombase.dll)的例外:0xc0000005:访问违规写作位置0x00000000000000000008。

edit :有时我会得到这个例外:

在MyApp.exe中丢弃在0x00007FF88825283BD(WindowsCodecs.dll):0xc0000005:访问违规位置0xfffffffffffffffffffffff.

它发生在此行:

bitmapSource.CopyPixels(stride, buffer);

我在某个地方阅读了FormatConverter所需的所有组件,因此在复制到缓冲区结果中以无效的读取之前将它们处置。之后它起作用了,但随后又没有任何有意义的理由破裂。现在,我总是得到此写入访问违规,我找不到来源。

我试图弄清楚使用拆卸的位置,但是我不知道0xC0000005的位置。毕竟,我不明白为什么这不起作用。该代码来自旧的SharpDx示例代码,即使在Stackoverflow上也被引用了很多。但这根本不起作用。基础FileStream具有 readwrite -Access,并且将DataStream设置为允许读取和写入操作。

这可能是sharpdx.wic codebase中的问题?

要保持简短而方便,这是按时间顺序排列的完整来源。

我的猜测是有些数字与指针混合在一起。查看记忆的地址,试图访问它:

  • 0x0000000000000008
  • 0xfffffffffffffffffffffffffffffffffff

以整数形式,这些是8和-1,因此,如果代码中的某人在API期望INTPTR或此类情况的情况下,您将不会令我感到惊讶。我无法访问您正在使用的特定图书馆,但是我会非常仔细地研究参数类型,并确保您没有传递它不期望的东西(或者是否有您将传递的任何位置为-1或8。)

最新更新