Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ DirectX小型应用程序消耗大量资源_C++_Winapi_Resources_Directx 9_Consumption - Fatal编程技术网

C++ DirectX小型应用程序消耗大量资源

C++ DirectX小型应用程序消耗大量资源,c++,winapi,resources,directx-9,consumption,C++,Winapi,Resources,Directx 9,Consumption,我正在尝试学习一些DirectX API,现在,我只有一个WINAPI窗口和一个简单的渲染函数,可以在整个屏幕上显示位图。我的主要功能: LPDIRECT3D9 pD3D; // the Direct3D object LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device // This is winmain, the main entry point for Windows applications int WINAPI WinMain(

我正在尝试学习一些DirectX API,现在,我只有一个WINAPI窗口和一个简单的渲染函数,可以在整个屏幕上显示位图。我的主要功能:

LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device

// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
                    int nCmdShow) {
     hInst = hInstance;
     // Initialize the window
     if (!initWindow(hInstance)) return false;
     // called after creating the window
     if (!initDirect3D()) return false;
     // main message loop:
     MSG msg;
     ZeroMemory( &msg, sizeof( msg ) );
     while( msg.message != WM_QUIT) {
         if( PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
             TranslateMessage ( &msg );
             DispatchMessage ( &msg );
         } else {
             render();
         }
     }
     // Release the device and the Direct3D object
     if( pd3dDevice != NULL ) pd3dDevice->Release( );
     if( pD3D != NULL ) pD3D->Release( );
     return (int) msg.wParam;
}
void render(void) {
    IDirect3DSurface9* surface;
    pd3dDevice->CreateOffscreenPlainSurface(640, // the width of the surface to create
        480, // the height of the surface to create
        D3DFMT_X8R8G8B8, // the surface format
        D3DPOOL_DEFAULT, // the memory pool to use
        &surface, // holds the resulting surface
        NULL); // reserved
    D3DXLoadSurfaceFromFile(surface, NULL, NULL, L"test.bmp", NULL, D3DX_DEFAULT, 0, NULL);
    // This will hold the back buffer
    IDirect3DSurface9* backbuffer = NULL;
    // Check to make sure you have a valid Direct3D device
    if( NULL == pd3dDevice ) return;// Clear the back buffer to a blue color
    pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
    // Get the back buffer
    pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer );
    // Copy the offscreen surface to the back buffer
    // Note the use of NULL values for the source and destination RECTs
    // This ensures a copy of the entire surface to the back buffer
    pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE );
    // Present the back buffer contents to the display
    pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
这是我的渲染函数:

LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device

// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
                    int nCmdShow) {
     hInst = hInstance;
     // Initialize the window
     if (!initWindow(hInstance)) return false;
     // called after creating the window
     if (!initDirect3D()) return false;
     // main message loop:
     MSG msg;
     ZeroMemory( &msg, sizeof( msg ) );
     while( msg.message != WM_QUIT) {
         if( PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
             TranslateMessage ( &msg );
             DispatchMessage ( &msg );
         } else {
             render();
         }
     }
     // Release the device and the Direct3D object
     if( pd3dDevice != NULL ) pd3dDevice->Release( );
     if( pD3D != NULL ) pD3D->Release( );
     return (int) msg.wParam;
}
void render(void) {
    IDirect3DSurface9* surface;
    pd3dDevice->CreateOffscreenPlainSurface(640, // the width of the surface to create
        480, // the height of the surface to create
        D3DFMT_X8R8G8B8, // the surface format
        D3DPOOL_DEFAULT, // the memory pool to use
        &surface, // holds the resulting surface
        NULL); // reserved
    D3DXLoadSurfaceFromFile(surface, NULL, NULL, L"test.bmp", NULL, D3DX_DEFAULT, 0, NULL);
    // This will hold the back buffer
    IDirect3DSurface9* backbuffer = NULL;
    // Check to make sure you have a valid Direct3D device
    if( NULL == pd3dDevice ) return;// Clear the back buffer to a blue color
    pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
    // Get the back buffer
    pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer );
    // Copy the offscreen surface to the back buffer
    // Note the use of NULL values for the source and destination RECTs
    // This ensures a copy of the entire surface to the back buffer
    pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE );
    // Present the back buffer contents to the display
    pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
因此,主要的问题是,当启用渲染函数(未注释)时,应用程序使用的内存会在一秒钟内达到400-600 Mb。现在,如果我禁用(注释)WinMain中的行,内存将保留5 Mb,但处理器会变得疯狂,应用程序会使用大约50%的内存。因此,看起来
WinMain()
使用了大量处理器和
render()
大量内存。为什么?我忘了什么

谢谢

pd3dDevice->CreateOffscreenPlainSurface(640, // the width of the surface to create
        480, // the height of the surface to create
        D3DFMT_X8R8G8B8, // the surface format
        D3DPOOL_DEFAULT, // the memory pool to use
        &surface, // holds the resulting surface
        NULL); // reserved
    D3DXLoadSurfaceFromFile(surface, NULL, NULL, L"test.bmp", NULL, D3DX_DEFAULT, 0, NULL);
您正在调用这段代码,它创建了无数次的资源,但您并没有发布它。它位于渲染函数中,必须每秒至少调用60次(如果它缺少与垂直回溯的同步,则每秒可以调用数千次,这给您带来了巨大的问题)。这意味着您将指向曲面的指针更改为具有相同映像的新曲面内存块,并将地址丢失为旧的(未释放)。因此,它会导致内存泄漏

将该代码转换为应用程序的初始化函数,而不是渲染函数。(并确保您能够管理它!当您停止使用它时,请调用release函数以降低COM对象的引用计数,以便系统可以占用内存(同样,您也可以使用))

编辑:这也需要移动到应用程序的init,在这里只需要一次

IDirect3DSurface9* backbuffer = NULL;
    // Check to make sure you have a valid Direct3D device
    if( NULL == pd3dDevice ) return;// Clear the back buffer to a blue color
    pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
    // Get the back buffer
    pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer );
    // Copy the offscreen surface to the back buffer
    // Note the use of NULL values for the source and destination RECTs
    // This ensures a copy of the entire surface to the back buffer
    pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE );
    // Present the back buffer contents to the display
您正在调用这段代码,它创建了无数次的资源,但您并没有发布它。它位于渲染函数中,必须每秒至少调用60次(如果它缺少与垂直回溯的同步,则每秒可以调用数千次,这给您带来了巨大的问题)。这意味着您将指向曲面的指针更改为具有相同映像的新曲面内存块,并将地址丢失为旧的(未释放)。因此,它会导致内存泄漏

将该代码转换为应用程序的初始化函数,而不是渲染函数。(并确保您能够管理它!当您停止使用它时,请调用release函数以降低COM对象的引用计数,以便系统可以占用内存(同样,您也可以使用))

编辑:这也需要移动到应用程序的init,在这里只需要一次

IDirect3DSurface9* backbuffer = NULL;
    // Check to make sure you have a valid Direct3D device
    if( NULL == pd3dDevice ) return;// Clear the back buffer to a blue color
    pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
    // Get the back buffer
    pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer );
    // Copy the offscreen surface to the back buffer
    // Note the use of NULL values for the source and destination RECTs
    // This ensures a copy of the entire surface to the back buffer
    pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE );
    // Present the back buffer contents to the display

处理器使用率为50%主要是由于频繁调用while循环无限次

例如

这将经常检查,因为您正在指示处理器在条件为真时将其集中。如果您使用的是4核处理器,其中一个处理器将完全专注于此过程,在我的例子中,我使用了4核,因此我的cpu使用率为25%。我认为您使用的是双核处理器

while(TRUE)
{
Sleep(1);
}

这将解决您的处理器问题。

处理器使用率为50%主要是由于频繁调用while循环无限次

例如

这将经常检查,因为您正在指示处理器在条件为真时将其集中。如果您使用的是4核处理器,其中一个处理器将完全专注于此过程,在我的例子中,我使用了4核,因此我的cpu使用率为25%。我认为您使用的是双核处理器

while(TRUE)
{
Sleep(1);
}

这将解决您的处理器问题。

谢谢!现在,使用的内存是50MB(它只加载一个900KB的位图),处理器总是50%。@ali阅读了我答案中的编辑,还有一件事需要做。@ali,
peek消息
没有引入空闲时间。为什么你想让一个游戏(你有一个游戏循环)闲置?谢谢!现在,使用的内存是50MB(它只加载一个900KB的位图),处理器总是50%。@ali阅读了我答案中的编辑,还有一件事需要做。@ali,
peek消息
没有引入空闲时间。为什么你想让一个游戏(你有一个游戏循环)无所事事?