Я начал создавать простой игровой движок с использованием Direct2D, но столкнулся с проблемой, возникающей при каждом выпуске IWICImagingFactory*. Проблема возникает только при закрытии программы.
Соответствующие части кода:
// GameEngine.cpp
// Creating the objects (Called after window is shown)
HRESULT hr;
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
HRException::CheckHR(hr);
RECT rc;
GetClientRect(hWnd, &rc);
hr = pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(hWnd,
D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)),
&pRT);
HRException::CheckHR(hr);
// Make Brushes
hr = pRT->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &pBlackBrush);
HRException::CheckHR(hr);
// Create image factory
hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pWICFactory));
HRException::CheckHR(hr);
// Releasing the objects (called when window is closed with WM_DESTROY)
void GameEngine::Destroy()
{
drawReady = false;
SafeRelease(pWICFactory); // Error is here
SafeRelease(pBlackBrush);
SafeRelease(pRT);
SafeRelease(pD2DFactory);
UnregisterClass(CLASS_NAME, hInst);
CoUninitialize();
}
// Loading image
GameEngine::Image::Image(const wchar_t *filePath)
{
if (!Get().drawReady)
return;
// Load the image using WIC
IWICBitmapDecoder *pDecoder = nullptr;
HRESULT hr = Get().pWICFactory->CreateDecoderFromFilename(filePath, nullptr, GENERIC_READ, WICDecodeOptions::WICDecodeMetadataCacheOnDemand, &pDecoder);
HRException::CheckHR(hr);
// Get the first frame of the image
IWICBitmapFrameDecode *pFrame = nullptr;
hr = pDecoder->GetFrame(0, &pFrame);
HRException::CheckHR(hr);
// Convert the image to a Direct2D-compatible pixel format
IWICFormatConverter* pConverter = nullptr;
hr = Get().pWICFactory->CreateFormatConverter(&pConverter);
HRException::CheckHR(hr);
// Initialize the format converter
hr = pConverter->Initialize(pFrame, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeErrorDiffusion, nullptr, 0.0f, WICBitmapPaletteTypeMedianCut);
HRException::CheckHR(hr);
// Create Direct2D bitmap from the converted WIC image
hr = Get().pRT->CreateBitmapFromWicBitmap(pConverter, nullptr, &pBitmap);
HRException::CheckHR(hr);
// Release the resources
SafeRelease(pFrame);
SafeRelease(pDecoder);
SafeRelease(pConverter);
}
Я пытался исключить любое использование IWICImagingFactory*, кроме его создания, не загружая изображения, но проблема не устранена. Изображения по-прежнему загружаются нормально. Я также убедился, что указатель не равен нулю перед вызовом Release().
Я начал создавать простой игровой движок с использованием Direct2D, но столкнулся с проблемой, возникающей при каждом выпуске IWICImagingFactory*. Проблема возникает только при закрытии программы. Соответствующие части кода: [code]// GameEngine.h template inline void SafeRelease( Interface *&pInterfaceToRelease) { if (pInterfaceToRelease != nullptr) { pInterfaceToRelease->Release(); pInterfaceToRelease = nullptr; } }
// In GameEngine class private: void Create(HINSTANCE hInst, int nCmdShow, const wchar_t *title, int width, int height);
// Releasing the objects (called when window is closed with WM_DESTROY) void GameEngine::Destroy() { drawReady = false; SafeRelease(pWICFactory); // Error is here SafeRelease(pBlackBrush); SafeRelease(pRT); SafeRelease(pD2DFactory); UnregisterClass(CLASS_NAME, hInst); CoUninitialize(); }
// Loading image GameEngine::Image::Image(const wchar_t *filePath) { if (!Get().drawReady) return;
// Load the image using WIC IWICBitmapDecoder *pDecoder = nullptr; HRESULT hr = Get().pWICFactory->CreateDecoderFromFilename(filePath, nullptr, GENERIC_READ, WICDecodeOptions::WICDecodeMetadataCacheOnDemand, &pDecoder); HRException::CheckHR(hr);
// Get the first frame of the image IWICBitmapFrameDecode *pFrame = nullptr; hr = pDecoder->GetFrame(0, &pFrame); HRException::CheckHR(hr);
// Convert the image to a Direct2D-compatible pixel format IWICFormatConverter* pConverter = nullptr; hr = Get().pWICFactory->CreateFormatConverter(&pConverter); HRException::CheckHR(hr);
// Initialize the format converter hr = pConverter->Initialize(pFrame, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeErrorDiffusion, nullptr, 0.0f, WICBitmapPaletteTypeMedianCut); HRException::CheckHR(hr);
// Create Direct2D bitmap from the converted WIC image hr = Get().pRT->CreateBitmapFromWicBitmap(pConverter, nullptr, &pBitmap); HRException::CheckHR(hr);
// Release the resources SafeRelease(pFrame); SafeRelease(pDecoder); SafeRelease(pConverter); } [/code] Я пытался исключить любое использование IWICImagingFactory*, кроме его создания, не загружая изображения, но проблема не устранена. Изображения по-прежнему загружаются нормально. Я также убедился, что указатель не равен нулю перед вызовом Release().