每次调整窗口大小时内存都会增加 [英] Memory increases every time I resize the window

查看:22
本文介绍了每次调整窗口大小时内存都会增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的 Win32 应用程序并尝试用颜色填充客户区.当包含Clear RenderTarget"这一行时,我看到每次调整窗口大小时内存都会增加几 KB.

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.

我的 WindowProc:

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

创建图形资源

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.

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.

我的操作系统是 Windows 10 x64

My OS is Windows 10 x64

推荐答案

问题似乎已解决.实际上内存并没有增加太多,所以似乎操作系统分配了其他内存.

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.

这篇关于每次调整窗口大小时内存都会增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆