0

Here's my problem: I'm working on a test project, that initializes Direct2D to draw some stuff.

For now, I am in need of creating BMP background so I looked some tutorials of loading bmp to use it with Direct2D on MSDN site. After some coding and debugging I came to the only problem I can't fully comprehend and, well, I'm stuck here. The problem is simple: I get ACCESS VIOLATION at this line: pRenderTarget->CreateBitmapFromWicBitmap( pConverter, NULL, ppBitmap); I fixed all issues I could find out and tested every HRESULT.

here's full code of background.cpp: `

#include "Background.h"
using namespace D2D1;
Background::Background()
{
    pIWICFactory = nullptr;
    pDecoder = nullptr;
    pSource = nullptr;
    pStream = nullptr;
    pConverter = nullptr;
    ppBitmap = nullptr;
    destinationWidth = 0;
    destinationHeight = 0;
    file_path = L"Background.bmp";
}
bool Background::init(HWND hwnd)
{
    CoInitializeEx(0, COINIT_MULTITHREADED);
    CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pIWICFactory));
    pIWICFactory->CreateDecoderFromFilename(file_path, NULL, GENERIC_READ, WICDecodeMetadataCacheOnLoad, &pDecoder);
    pIWICFactory->CreateStream(&pStream);
    pStream->InitializeFromFilename(file_path, GENERIC_READ);
    pDecoder->Initialize(pStream,WICDecodeMetadataCacheOnLoad);
    pDecoder->GetFrame(0, &pSource);
    pIWICFactory->CreateFormatConverter(&pConverter);
    pConverter->Initialize(pSource, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut);
    pRenderTarget->CreateBitmapFromWicBitmap( pConverter, NULL, ppBitmap);
    return true;
}
Background::~Background()
{
    CoUninitialize();
    pIWICFactory->Release();
    pDecoder->Release();
    pSource->Release();
    pStream->Release();
    pConverter->Release();
    CoUninitialize();
}

`

And here's parent class "Render.cpp" (there's no connection between how I managed class inheritance and the problem - I tried to create new project that contained only one class including both render and background)

`

#include "Render.h"
using namespace D2D1;
Render::Render()
{
    pD2DFactory = nullptr;
    pRenderTarget = nullptr;
    pGreenBrush = nullptr;
}

bool Render::Init(HWND hwnd)
{
    HRESULT hr = D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory );
    RECT rc;
    GetClientRect(hwnd, &rc);
    hr = pD2DFactory->CreateHwndRenderTarget(RenderTargetProperties(), HwndRenderTargetProperties(hwnd, SizeU( rc.right - rc.left, rc.bottom - rc.top)),&pRenderTarget);
    if (SUCCEEDED(hr))          
      pRenderTarget->CreateSolidColorBrush(ColorF(ColorF::Green), &pGreenBrush ); 
    else
      return false;
    return true;
}

bool Render::Draw(HWND hwnd)
{
    RECT rc;
    GetClientRect(hwnd, &rc);

    pRenderTarget->BeginDraw();

    pRenderTarget->FillRectangle(
        RectF(
            rc.left + 500.0f,
            rc.top + 250.0f,
            rc.right - 500.0f,
            rc.bottom - 500.0f),
            pGreenBrush);   

    HRESULT hr = pRenderTarget->EndDraw();  

    if (SUCCEEDED(hr))
        return true;
    else
        return false;
}
void Render::ShutDown()
{
    if (pD2DFactory)
        pD2DFactory->Release();
    if (pRenderTarget)
        pRenderTarget->Release();
    if (pGreenBrush)
        pGreenBrush->Release();
}

`

1 Answers1

0

From the double 'pp' I asume you declared ppBitmap; as D2D1Bitmap**, also because you pass it as-value to CreateBitmapFromWicBitmap.

If correct your solution is simple: Declare a ptrP*, not a ptrptrPP**

// in class declaration or c'tor(?)
D2D1Bitmap* pBitmap = nullptr;      // instead of D2D1Bitmap**

/// at the point of creation
D2D1wndTarget->CreateBitmapFromWicBitmap(<yourConverter>, NULL, &pBitmap);

In your example you declared it as null-ptr and passed it to a function that takes an address to pointer, resulting in the function reading at 0x000000 resulting in an access violation.

BeschBesch
  • 373
  • 2
  • 11