SharpShade
SharpShade

Reputation: 2163

Find origin of access violation in Mixed Code

I got the following code (using SharpDX.WIC / SharpDX.Direct3D11) which always throws an exception saying there was an access violation.

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));
    }
}

The exception message is:

Exception thrown at 0x00007FF894ECA25C (combase.dll) in MyApp.exe: 0xC0000005: Access violation writing location 0x0000000000000008.

EDIT: Sometimes I get this exception instead:

Exception thrown at 0x00007FF8825283BD (WindowsCodecs.dll) in MyApp.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

It occurs at this line:

bitmapSource.CopyPixels(stride, buffer);

I read somewhere that the FormatConverter requires all components, so disposing them before copying to the buffer results in invalid reads. Afterwards it worked, but then broke again without any meaningful reason. Now I always get this write access violation and I can't find the source.

I tried to figure out where it happens using the disassembly, but I have no clue where 0xC0000005 would be located. After all I don't get why this doesn't work. The code for this was from an older SharpDX sample code which has been referenced to a lot even on Stackoverflow. But it simply doesn't work. The underlying FileStream has ReadWrite-access and the DataStream is set to allow read and write operations.

Might this be an issue in the SharpDX.WIC codebase?

To keep this short and handy, here is the full source in chronological call-order.

Upvotes: 1

Views: 971

Answers (1)

Kevin
Kevin

Reputation: 2243

My guess is that some numbers got conflated with pointers. Look at the memory addresses its trying to access:

  • 0x0000000000000008
  • 0xFFFFFFFFFFFFFFFF

In integer form, these are 8 and -1, so it wouldn't surprise me if someone in the code, you're passing in numerical values where the API is expecting an IntPtr or such. I don't have access to the particular library you're working with, but I'd take a very close look at what the argument types are and make sure you're not passing something it doesn't expect (or if there are any places you'd be passing a value of -1 or 8.)

Upvotes: 1

Related Questions