0

这是我要插入的类。它有构造函数初始化'm_bitmap'。事实上,初始化 m_bitmap 不会导致问题。我插入细节。

#pragma once
#include "D2DApp.h"

class Image
{
public:
    ComPtr<ID2D1Bitmap> m_bitmap;
    Image(LPCWSTR filePath);
};

它是构造函数

Image::Image(LPCWSTR filePath)
{
    //Create Decoder
    ComPtr<IWICBitmapDecoder> bitmapDecoder;
    ThrowIfFailed(
        D2DApp::GetApp()->
        GetWicFactory()->CreateDecoderFromFilename(
            filePath,
            NULL,
            GENERIC_READ,
            WICDecodeMetadataCacheOnLoad,
            bitmapDecoder.GetAddressOf()
        )
    );

    //Get the current frame
    ComPtr<IWICBitmapFrameDecode> frame;
    ThrowIfFailed(
        bitmapDecoder->GetFrame(0, frame.GetAddressOf())
    );

    //Create the format converter
    ComPtr<IWICFormatConverter> wicImage;
    ThrowIfFailed(
        D2DApp::GetApp()->GetWicFactory()->CreateFormatConverter(
            wicImage.GetAddressOf()
        )
    );

    //Initialize wic Image
    ThrowIfFailed(
        wicImage->Initialize(
            frame.Get(),
            GUID_WICPixelFormat32bppPBGRA,
            WICBitmapDitherTypeNone,
            nullptr,
            0,
            WICBitmapPaletteTypeCustom
        )
    );

    //Initialize D2d image
    ThrowIfFailed(
        D2DApp::GetApp()->GetD2DContext()->
        CreateBitmapFromWicBitmap(
            wicImage.Get(),
            m_bitmap.ReleaseAndGetAddressOf()
        )
    );
}

然后我将它插入到地图容器中

#include "Image.h"
#include <map>

class ImageSystem
{
private:
    map<string, Image*> ImageData;
public:
    void addData(string key, LPCWSTR filePath);
    ComPtr<ID2D1Bitmap> GetData(string key);
    ~ImageSystem();
};

问题发生在这里,两行代码由于相同的原因没有工作。

抛出异常:读取访问冲突。_Scary 是 nullptr。

void ImageSystem::addData(string key, LPCWSTR filePath)
{
    //ImageData.insert(make_pair(key, new Image(filePath)));
    ImageData[key] = new Image(filePath);
}
4

0 回答 0