No announcement yet.

MemoryDC Question

  • Filter
  • Time
  • Show
Clear All
new posts

  • MemoryDC Question


    If you create a memory DC and have some objects selected into it, do you still have to restore the default objects if you are just going to delete the memory DC anyway?

    Just wondering...


  • #2
    From my memDC notes...

    Later on when the memory DC and bitmap are to be destroyed, the
    old bitmap "MUST" be selected back into the memory DC, then the
    memory DC and bitmap can be deleted. On the other hand, I have
    also NOT selected the old bitmap back into the memory DC and
    nothing fails or leaks. Probably you should do it, just to give
    you that false sense of security.



    • #3

      I doubt you will have any problems with leaving objects selected
      into a memory DC. While it is always a good practice to restore
      the old objects to any DC, a memory DC is private and not shared
      by other processes, so you don't have to worry about the kind of
      problems normally experienced with a regular DC.

      A regular DC (not a mem DC or printer DC) is actually associated
      with the video adapters memory (what you see on the screen). Screen
      DC's can be shared. For example a Dialog may have its own private DC
      or it may share the DC of the desktop. Controls can have their own
      private DC (not usually) or they can share the DC of its parent
      window. This is why it is so important to restore the old objects
      of a screen DC, since another window may be using the same DC.

      As rule though, I would suggest to always "play it safe" and restore
      the old objects to a memory DC before destroying it. You can create
      a memory DC when a window is created (WM_CREATE) and then destroy it
      when the window is destroyed (WM_DESTROY). You only have to clean up
      the DC just before you destroy it.

      Chris Boss
      Computer Workshop
      Developer of "EZGUI"


      • #4

        Its good practice to be symetrical with device contexts, when
        you access an existing one, GetDC(), when you finish your
        operation, ReleaseDC().

        If you are making your own, CreateDC() and when you have finished
        with it, DeleteDC().

        The one that leaves you with memory leaks is SelectObject(). Always
        save the old "object" when you se;ect a new one and when you have
        finished with it, destroy it and select the old one back again.

        There area times when you can get away with being untidy but it will
        usually come back and haunt you in the middle of a complex app where
        the problem is hard to find. To GDI objects, just apply the old rule,
        what goes up must come down.


        [email protected]

        hutch at movsd dot com
        The MASM Forum - SLL Modules and PB Libraries


        • #5
          Thanks Guys!

          The main reason I ask is because I was writing some custom controls and I noticed they flickered a bit. So, I decided to use an old DOS trick and double-buffer the controls painting. This is where the memory DC came in. I could draw my controls surface in the memory DC and then BLIT it to the main window. Works great but I just wanted to make sure about the finer details.

          Thanks again