Thread: Device Contexts and memory leaks.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Code:
    //get a copy of the screen DC
    HDC hdc = GetDC(NULL); //NULL is the main screen IIRC, might want to check this
    //and create our memDC
    HDC hdcMem = CreateCompatibleDC(hdc); 
    HBITMAP A = LoadBitmap(GetModuleHandle(NULL), "Somepicture.bmp"); 
    HBITMAP B = LoadBitmap(GetModuleHandle(NULL), "Anotherpicture.bmp"); 
    //variable to hold the default 1x1 BITMAP already in the DC you created
    HBITMAP* origBMP = NULL;
    
    //'catch' the default BITMAP pushed out by you selecting a BITMAP into the DC
    origBMP = (HBITMAP*)SelectObject(hdcMem, A); 
    //draw
    BitBlt(hdc, 0, 0, 32, 32, hdcMem, 0, 0, SRCCOPY); 
    //when we select BITMAP 'B' into the DC, BITMAP 'A' is pushed out. 
    //as we already have A in a variable, we do not need to catch it
    SelectObject(hdcMem, b);  
    BitBlt(hdc, 32, 0, 32, 32, hdcMem, 0, 0, SRCCOPY); 
    //clean up
    //select the default BITMAP back into the DC
    //BITMAP 'B' will be pushed out, as we have this in a variable no need to catch it
    SelectObject(hdcMem, origBMP);
    
    //now memDC is contains the same GDI objects as when we creatd it we can delete it
    DeleteDC(hdcMem);
    
    //delete anything we created
    DeleteObject(A);
    DeleteObject(B);
    
    //release anything we 'GetDC'ed
    ReleaseDC(hdc);
    >>Obviously in a real program i would not call a LoadBitmap() function everytime i drew. I would make it part of the game class construction. So if i deleted these HBITMAPS before the WM_DESTROY i am guessing i would lose the Handle to the actual files.

    Yes. Make these have scope as long as your window is on screen. Create the GDIs when the window opens and when the window closes, select in the original GDIs and delete all GDIs you created.

    >>GetLastError() returns an int is there a way to output this to the debugger so I can check this value?

    int iErr = GetLastError()
    Zero is ERROR_SUCCESS (no error) so you can write conditional code (using #ifdef __DEBUG)
    Conditional Compilation (#if, #ifdef, #ifndef, #else,…#elif, #endif, and defined)
    or put a conditional break point on the line (ie iErr != 0)
    or put a watch on iErr in the debugger
    or output to a MessageBox (possibly using FormatMessage() ).

    See this for a code snippet;
    Retrieving the Last-Error Code (Windows)

    What the codes mean
    System Error Codes (Windows)

    I will try to find the time to read your code and comment.
    Last edited by novacain; 11-08-2012 at 11:26 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-15-2012, 05:16 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  4. Memory leaks!!!
    By kaushik1986 in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2007, 05:41 AM
  5. Drag and Drop using CImageList, Device contexts and BitBlt
    By MJWhiteman2 in forum Windows Programming
    Replies: 0
    Last Post: 03-03-2005, 07:22 AM