Search code examples
c++visual-studio-2010comatldirect2d

Direct2d CreateBitmapfromWICBitmap giving assert error within ATL


I have a simple function that loads a Png file and returns it as a ID2D1Bitmap. But when it tries to call the CreateBitmapfromWicBitmap function, it gives a debug assert error. The funny thing is that I first made an imageload function in a seperate project, and it works fine in there. Both of these functions have the same code, while the second one is giving errors.

Here's the erroring code:

ID2D1Bitmap* Wnd::LoadPng(LPCWSTR Path) {
    CComPtr<IWICBitmapDecoder> pDecoder;
    CComPtr<IWICBitmapFrameDecode> pFrame;
    CComPtr<ID2D1Bitmap> pBit;
    CComPtr<IWICFormatConverter> pConv;
    HRESULT Hr;

    Hr = m_pWICFactory->CreateDecoderFromFilename(Path,NULL,GENERIC_READ,WICDecodeMetadataCacheOnDemand,&pDecoder);

    if (SUCCEEDED(Hr)) {
        Hr = m_pWICFactory->CreateFormatConverter(&pConv);
    }

    if (SUCCEEDED(Hr)) {
        Hr = pDecoder->GetFrame(0,&pFrame);
    }

    if (SUCCEEDED(Hr)) {
        Hr = pConv->Initialize(pFrame,GUID_WICPixelFormat32bppPBGRA,WICBitmapDitherTypeNone,0,0.f,WICBitmapPaletteTypeCustom);
    }

    if (SUCCEEDED(Hr)) {
        Hr = m_pRT->CreateBitmapFromWicBitmap(pConv,0,&pBit);
    }
    return pBit;
}

The error happens in atlcomcli.h at line 182 in function _NoAddRefReleaseOnCComPtr.

I double-checked all headers and libraries and they're the same in both projects (With some extra headers in the second project).

Here's the code that WORKS:

        CComPtr<IWICFormatConverter> Conv;
        m_pWICFactory->CreateFormatConverter(&Conv);
        CComPtr<IWICBitmapFrameDecode> Frame;
        m_pDecoder->GetFrame(0,&Frame);
        Frame->GetSize(&W,&H);
        Conv->Initialize(Frame,GUID_WICPixelFormat32bppPBGRA,WICBitmapDitherTypeNone,0,0.f,WICBitmapPaletteTypeCustom);
        CComPtr<ID2D1Bitmap> Bit;
        Hr = m_pRT->CreateBitmapFromWicBitmap(Conv,0,&Bit);
        m_pBitmap.push_back(Bit);

BitmapDecoder is predefined here, but it's exactly the same as in the first snippet.

------------------------------- FIXED ----------------------------

Third time I forgot to call the init function for my rendertarget.


Solution

  • The assertion failure warns you that you are trying to "use" a NULL interface pointer through CComPtr template. You should look up on the call stack which exactly line of your code you are at, and what variable holds NULL pointer which you expect to be non-NULL. Or otherwise just step through your code with debugger.