Thread: Deleting HDC w/ bitmap selected

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Deleting HDC w/ bitmap selected

    Ok, this is some code that was included in a DirectX utility file thinger:
    Code:
    HDC                     hdcImage;
    //some stuff not involving hdcImage
    hdcImage = CreateCompatibleDC(NULL);
    //if hdcImage is null, do something
    SelectObject(hdcImage, hbm); //hbm is a HBITMAP passed to the function
    //some stuff, only involving hdcImage for a StretchBlt()
    DeleteDC(hdcImage);
    If you select an object into an HDC, aren't you supposed to store the returned object in a temp variable and then select it back in before you delete the HDC? Or does it not matter because hdcImage was created within the function?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Yes, it is proper to do so. It's also proper to DeleteObject on gdi resources when finished with them too. However, windows ensures that all such resources are killed when the process/app that created them is killed.

    I think that when you create a dc, the default bitmap is a 1x1 monochrome - but don't quote me on that.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Ken Fitlike
    I think that when you create a dc, the default bitmap is a 1x1 monochrome - but don't quote me on that.
    That is in fact true. So if you want to use the DC as a back buffer or something just create a compatible bitmap with correct size and select it into the dc.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, it is proper to do so
    Ok, thanks. But is it absolutely necessary? (i.e. otherwise will I get some of that very reliable "undefined behaviour"?)

    (...) DeleteObject on gdi resources (...)
    I would, but it's passed as a parameter, and it wouldn't be nice to kill somebody else's pet HBITMAP, would it?

    So if you want to use the DC as a back buffer or something just create a compatible bitmap with correct size and select it into the dc.
    I already knew that (I think), but thanks anyways
    Last edited by Hunter2; 12-07-2002 at 09:16 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>and it wouldn't be nice to kill somebody else's pet HBITMAP<<



    >>But is it absolutely necessary? <<

    I would say so as a matter of good practice if nothing else. Personally I would just as a matter of course - force of habit, if you will. I like to free up gdi resources asap after using them even though I understand that the system will do so when the process/app terminates. Still everyone seems to have their pet areas of sloppy coding, so whatever blits your bitmap, eh?

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    lol ok, I sort of get the point, but I'm still getting confused This is the psuedo-code:
    Code:
    create an HDC called hdcImage, compatible with the screen
    select a bitmap (that is passed as an argument) into it
    StretchBlt() it onto another HDC
    delete it
    My question is, should it be this instead:
    Code:
    create an HDC called hdcImage, compatible with the screen
    
    select a bitmap (that is passed as an argument) into it,
    --> and store the returned bitmap in "temp"
    
    StretchBlt() it onto another HDC
    
    --> select "temp" back in
    
    delete hdcImage
    Are the pointed-to lines needed? (i.e. otherwise would the original bitmap be deleted with it or something similarly sinister?)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    Are the pointed-to lines needed? (i.e. otherwise would the original bitmap be deleted with it or something similarly sinister?)
    this is the proper manor to this yes, else when you get rid of the dc the original bitmap isn't destroyed and thus causing memory leaks...
    one should always restore GDI-objects initial data before deleting them..

    /btq
    ...viewlexx - julie lexx

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Alright, thanks. But...
    else when you get rid of the dc the original bitmap isn't destroyed and thus causing memory leaks...
    Then does that mean that the currently selected bitmap is destroyed?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    No, AFAIK.

    An 'created' object will not be destroyed if currently selected into a DC. Either by a call to DestroyObject() ect on the object or the DC.

    The resource leak from you code would be minor. Probably not mattering unless the app has to run on low end machines or for extended periods.

    I would use your second example as a standard.

    Remember GDI memory <<< available RAM
    "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

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It is usually very noticable when you have a memory leak with your GDI code. Windows is sufficated and slowly dies (and you can watch it do so). Also, it isn't always necessary to delete stuff immediately however, not all computers are the same and some of them are older and/or have less memory. It is good practice to take evey one's machine into consideration.

  11. #11
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    Then does that mean that the currently selected bitmap is destroyed?
    no, it doesn't...
    and those resource leaks can be a problem...I once did an app
    that blited many many times a sec between several DC's and the
    memory leaks were horrible when not selecting the original
    bitmap into the DC before destroying it. could just sit and watch
    the app eat up all the ram


    /btq
    ...viewlexx - julie lexx

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, I appreciate all the replies (and I guess I'll stick to method #2 ), but one thing is still confusing me -

    -if I don't select the original bitmap back in before deleting the HDC, it won't be deleted...

    -but the currently selected bitmap won't be deleted either

    -Then the bitmap currently selected into the HDC isn't deleted when the HDC is

    -so even if the original bitmap is selected back in, it won't be deleted, will it??

    Or is there some special consideration for the original bitmap?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You don't delete the saved bitmap. Your sole responsibilty is to reselect it and the destroy the one you created.

    DeleteObject(SelectObject(hdc, saved));

    That applies to any and all GDI objects...except(!) for stock and system objects (HBITMAPS never come in those two flavors, but brushes, pens, fonts, etc. can). In those cases, just reselect the saved GDI object and leave the on you created alone...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, I guess I get it... basically, I mind my own business and let Windows mind its own too, right? I delete my own objects, and let Windows delete its own in its own way, and how it goes about doing that (whether it deletes it when the HDC is deleted or not) is none of my beeswax, right?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well put.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. My progress.. thnx to Cprogramming forumites
    By csonx_p in forum Windows Programming
    Replies: 6
    Last Post: 05-21-2008, 01:17 AM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM