d2d程序,需要通过文件加载图片,发现内存占用不断增大,好像不能立即释放。加载一个比较大的图片时,执行到语句pRenderTarget->CreateBitmapFromWicBitmap(),内存就会增加大约10M,每次加载一次图片,内存就增加一些,好像不会释放,测试了十几次内存占用达到500M左右。即便最后增加了 wicBitmapDecoder.Release(),wicFrameDecode.Release(), wicConverter.Release()也不起作用。
网上也有人遇到改问题,https://segmentfault.com/q/1010000004380144?bd_source_light=4746641,但是尝试将pRenderTarget Release后也还是不起作用。
HRESULT LoadStreamToBitmap(ID2D1RenderTarget* pRenderTarget, IStream* pStream, ID2D1Bitmap** ppBitmap)
{
CComPtr<IWICBitmapDecoder> wicBitmapDecoder;
CComPtr<IWICBitmapFrameDecode> wicFrameDecode;
CComPtr<IWICFormatConverter> wicConverter;
//从图像流创建解码器
HRESULT hr;
HR(hr = theApp.m_pWICFactory->CreateDecoderFromStream(
pStream,
nullptr,
WICDecodeMetadataCacheOnLoad,
&wicBitmapDecoder)
);
// 获取解码器的第一帧
HR(wicBitmapDecoder->GetFrame(0, &wicFrameDecode));
HR(theApp.m_pWICFactory->CreateFormatConverter(&wicConverter));
HR(wicConverter->Initialize(
wicFrameDecode,
GUID_WICPixelFormat32bppPBGRA,//即图片每个像素使用32位存储,图片透明度设置为0.0,即图片不透明。
WICBitmapDitherTypeNone,
nullptr,
0.0f,
WICBitmapPaletteTypeMedianCut)//WICBitmapPaletteTypeCustom?
);
//创建 ID2D1Bitmap 对象,该对象可通过呈现器目标绘制并与其他 Direct2D 对象一起使用
//create a Direct2D bitmap from the WIC bitmap.
HR(pRenderTarget->CreateBitmapFromWicBitmap(
wicConverter,
nullptr,
ppBitmap)
);
return S_OK;
}