7

Programming with C++, once we get the context device by GetDC to use. What bad things may happen if we exit the program without calling to ReleaseDC?

jondinham
  • 7,515
  • 15
  • 71
  • 130
  • 7
    mostly you just have to deal with the ensuing bad karma – David Heffernan Aug 28 '11 at 07:54
  • 4
    Nothing, Windows cleans up the litter when it tears down the process. Holding on a device context is however almost always wrong. It will be invalid when the window size changes for example. Creating a device context is very cheap, don't optimize too early. – Hans Passant Aug 28 '11 at 11:19

3 Answers3

4

From the docs

The ReleaseDC function releases a device context (DC), freeing it for use by other applications. The effect of the ReleaseDC function depends on the type of DC. It frees only common and window DCs. It has no effect on class or private DCs.

As you can see, it may be needed if other applications can access the same DC.

In any case, it's good idea to use C++ RAII idiom for this kind of things. Consider this class:

class ScopedDC
{
   public:
      ScopedDC(HDC handle):handle(handle){}
      ~ScopedDC() { ReleaseDC(handle); }
      HDC get() const {return handle; }
   //disable copying. Same can be achieved by deriving from boost::noncopyable
   private:
      ScopedDC(const ScopedDC&);
      ScopedDC& operator = (const ScopedDC&); 

   private:
      HDC handle;
};

With this class you can do this:

{
   ScopedDC dc(GetDC());
   //do stuff with dc.get();
}  //DC is automatically released here, even in case of exceptions
Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
4

Application with GDI-related resource leaks may stop drawing anything after some time of continuous running. All application windows remain empty, though internally GDI calls return success. "Application stops drawing" - this is standard question in Windows programming message boards, when program has resource leaks.

Alex F
  • 39,172
  • 34
  • 138
  • 200
2

What bad things may happen if we exit the program without calling to ReleaseDC?

Exiting the program automatically releases all allocated resources, so I agree with David Heffernan's and Hans Passant's comments: nothing bad will happen.

Exiting a routine in which you used GetDC is another story. As Armen Tsirunyan already pointed out, ReleaseDC múst be called after obtaining the device context of common and window DC's, otherwise the program might run in an out of resources exception due to the growth of the system's special cache for these DCs.

Now, whether the window you are obtaining the DC from is a common or window DC might be convenient to know. One way of getting this information is to see if the window has a private device context. See the documentation about Display Device Contexts:

Private Device Contexts

... Private device contexts are not part of the system cache and therefore need not be released after use. ... An application creates a private device context by first specifying the CS_OWNDC window-class style when it initializes the style member of the WNDCLASS structure and calls the RegisterClass...

Thus, when the window class has the CS_OWNDC flag set, you do not have to worry about releasing the DC because it is managed by the window itself rather then the system cache. You can obtain this setting by calling GetClassInfo(Ex).

But note that releasing the DC gotten by GetDC has no effect on class or private DCs, so it is always sensible to call ReleaseDC after GetDC.

Community
  • 1
  • 1
NGLN
  • 41,230
  • 8
  • 102
  • 186