本文介绍了什么坏事情可能发生,而不调用ReleaseDC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用C ++编程,一旦我们通过GetDC获取上下文设备使用。
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?
推荐答案
从
如你所见, ,如果其他应用程序可以访问同一个DC。
As you can see, it may be needed if other applications can access the same DC.
在任何情况下,最好使用C ++ 这种东西。考虑这个类:
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
这篇关于什么坏事情可能发生,而不调用ReleaseDC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!