What bad things may happen without calling to ReleaseDC?

旧巷老猫 提交于 2019-12-08 18:52:49

问题


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?


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7219640/what-bad-things-may-happen-without-calling-to-releasedc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!