Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:06:18+00:00 2026-06-06T04:06:18+00:00

I am trying to learn some DirectX API and, for now, I have just

  • 0

I am trying to learn some DirectX API and, for now, I have just a WINAPI window and a simple render function that displays a bitmap over the entire screen. My WINMAIN function:

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

and this is my render function:

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

So, the main problem is that when the render function is enabled (uncommented) the memory used by the application goes to 400 – 600 Mb in a second. Now, If I disabled (comment) the line from WinMain, the memory is left in 5 Mb but the processor goes crazy and the application uses about 50% of it. So, it looks like WinMain() uses a lot of processor and render() a lot of memory. Why? What am I forgetting?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:06:19+00:00Added an answer on June 6, 2026 at 4:06 am
    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);
    

    You’re calling this bit of code which creates resources a bazillion times, but you’re not releasing it. It’s in the render function which must be called at least 60 times a second (and if it lacks the sync with the vertical retrace, it can be called thousands of times a second, creating a huge problem for you). That means you change the pointer to the surface to a new block of surface memory with the same image and lose the address to the old (unreleased). Therefore, it causes a memory leak.

    Shift that code into an initialization function of the application, not in the render function. (and make sure you manage it! When you stop using it, call the release function to lower the ref. count of the COM object so the memory can be claimed by the system (available to you, again))

    Edit: This also needs to be moved to the init of the app, it’s only needed once here

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn some programming with jQuery. I have a div that has
Ok so I am just trying to learn some of this fun stuff now,
I'm trying to learn some javascript and have gotten stuck. Right now, I have
I am trying to learn some VB.NET for my coo-op that starts next week,
I'm trying to learn some C#.net. I'm just trying to expose the AdventureWorks database
I'm trying to do a little PDO CRUD to learn some PDO. I have
I am just trying to learn some Perl, so I am going through one
Hi I am trying to learn some function pointers in C/C++ and I was
I am trying to learn some simple matlab code posted by my professor, and
Me again, Trying to learn some jQuery stuff. Now I'm building an AJAX search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.