Results 1 to 6 of 6

Thread: ReleaseDC needed after GetDC?

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    ReleaseDC needed after GetDC?

    I've seen quite a few code snippets that always use ReleaseDC after using GetDC. Is this necessary?

    I'm just using GetDC() to get the device context of a window by its hWnd.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: ReleaseDC needed after GetDC?

    Can't hurt to release it.

    After painting with a common device context, the ReleaseDC function must be called to release the device context.
    http://msdn.microsoft.com/en-us/library/aa921543.aspx

    After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is limited only by available memory.
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: ReleaseDC needed after GetDC?

    When in doubt, check the documentation on MSDN
    Quote Originally Posted by msdn
    ...After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is limited only by available memory.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: ReleaseDC needed after GetDC?

    That's why I was asking, I didn't understand the documentation... I don't know the difference between private and common DC?

    And there is no painting going on... just using it to get the DC handle by hWnd.

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: ReleaseDC needed after GetDC?

    I don't know the difference between private and common DC?
    Common DC.
    The common DC encapsulates two types of DCs, the window DC and the client DC. Common DCs are created from a pool of DCs that is limited by the amount of memory on the current system. Common DCs are further classified as Client and Window DCs. Client and Window DCs are very similar. The only difference between the two is that a client DC is restricted to the client area of the window, while the window DC gives the application access to both the client and the non-client areas of the window.

    Class / Private DCs
    It is important not to cache the common DCs in an application. How then can an application create a DC that does not have to be reinitialized each time it is called? This can be done with either a class or private DC. These two DCs can be retrieved in the same manner as the other common DCs and they do not need to be released. However it is good practice to release these DCs as the release functions have no effect, and memory leaks will be prevented if the DCs are ever converted back to common DCs. Class and private DCs are persistent, therefore the state of the DC will remain the same as it was in the previous call to that DC. This will allow a DC to be initialized once. This could save valuable time for applications that have lengthy DC initializations. Given below is a short description of both the class and private DC.
    Class DC : A class DC is created for use for a single window class.
    Private DC : A private DC is created for one particular window.

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: ReleaseDC needed after GetDC?

    Quote Originally Posted by DigiRev View Post
    <snip>I'm just using GetDC() to get the device context of a window by its hWnd.
    Quote Originally Posted by DigiRev View Post
    <snip>there is no painting going on... just using it to get the DC handle by hWnd.
    I don't think whether or not you are painting matters. I think () in the context of a common DC (like the one retrieved via a hWnd), the DC is shared so there needs to be some sort of system to stop it getting messed up.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width