用C++编程,一旦我们通过GetDC获得context device就可以使用了。如果我们不调用ReleaseDC就退出程序会发生什么不好的事情?
最佳答案
来自 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.
如您所见,如果其他应用程序可以访问同一 DC,则可能需要它。
无论如何,最好使用 C++ RAII idiom对于这种事情。考虑这个类:
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;
};
使用这个类你可以这样做:
{
ScopedDC dc(GetDC());
//do stuff with dc.get();
} //DC is automatically released here, even in case of exceptions
关于c++ - 不调用 ReleaseDC 可能会发生什么坏事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7219640/