Memory increases every time I resize the window

我的梦境 提交于 2019-12-12 05:26:10

问题


I have created a simple Win32 Application and try to fill the Client Area with a Color. When the line "Clear RenderTarget" is included I see that the memory increases a few KB every time I resize the window.

My 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;
}

Globals:

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

Do I miss something to free up memory or what could be the reason? If I resize the window e.g. 5 sec the memory goes from 4KB up to 22KB.

My OS is Windows 10 x64


回答1:


The issue seems to be solved. In fact the memory did not go up much more so it seems the OS assigns the other memory.



来源:https://stackoverflow.com/questions/34696469/memory-increases-every-time-i-resize-the-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!