每次调整窗口大小时内存都会增加

时间:2016-01-09 17:13:31

标签: c winapi direct2d

我创建了一个简单的Win32应用程序并尝试使用Color填充客户区。当包含“Clear RenderTarget”行时,我发现每次调整窗口大小时内存都会增加几KB。

我的WindowProc:

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_SIZE:
        {
            if (pRenderTarget != NULL)
            {
                RECT rc;
                GetClientRect(globalWindowHandle, &rc);

                D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);

                pRenderTarget->Resize(size);

                InvalidateRect(globalWindowHandle, NULL, FALSE);
            }
            return  0;
        }
        break;
        case WM_CREATE:
        {
            HRESULT dx = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pFactory);

            if (FAILED(dx))
            {
                MessageBox(globalWindowHandle, "Error creating D2D1Factory", "Error", MB_ICONERROR);
                return -1;
            }

            return 0;
        }
        break;
        case WM_KEYDOWN:
        {
            int ret = HandleKeyboardInput(uMsg, wParam, lParam);

            if (ret == 0)
            {
                return 0;
            }
        }
        break;
        case WM_PAINT:
        {
            HRESULT hr = CreateGraphicsResources();

            if (SUCCEEDED(hr))
            {

                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);

                pRenderTarget->BeginDraw();

                // Clear RenderTarget
                pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::SkyBlue));

                hr = pRenderTarget->EndDraw();

                if (FAILED(hr) || hr == D2DERR_RECREATE_TARGET)
                {
                    pRenderTarget->Release();
                    pSolidBrush->Release();
                    pRenderTarget = NULL;
                    pSolidBrush = NULL;
                }

                EndPaint(hwnd, &ps);

                return 0;
            }

        }
        break;
        case WM_CLOSE:
        {
            int box = MessageBox(hwnd, "Would you like to close the editor ?", "Question", MB_OKCANCEL);
            if (box == IDOK)
            {
                DestroyWindow(hwnd);
            }
            return 0;
        }
        break;
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
        break;
        default:
        {
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
        break;
    }

    return 0;
}

CreateGraphicsResources

HRESULT CreateGraphicsResources()
{
    HRESULT hr = S_OK;

    if (pRenderTarget == NULL)
    {
        RECT rc;
        GetClientRect(globalWindowHandle, &rc);

        D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);

        hr = pFactory->CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(globalWindowHandle, size),
            &pRenderTarget);

        if (SUCCEEDED(hr))
        {
            const D2D1_COLOR_F color = D2D1::ColorF(1.0f, 1.0f, 1.0f, 0);
            hr = pRenderTarget->CreateSolidColorBrush(color, &pSolidBrush);

            if (SUCCEEDED(hr))
            {

            }
        }


    }
    return hr;
}

全局:

BOOL ctrlPressed = FALSE;
HWND globalWindowHandle;
ID2D1Factory *pFactory;
ID2D1SolidColorBrush *pSolidBrush;
ID2D1HwndRenderTarget *pRenderTarget;

我是否会遗漏一些东西来释放记忆或者可能是什么原因?如果我调整窗口大小,例如5秒内存从4KB增加到22KB。

我的操作系统是Windows 10 x64

1 个答案:

答案 0 :(得分:1)

这个问题似乎已经解决了。事实上,内存并没有增加太多,所以操作系统似乎分配了另一个内存。