行规——::GetDC()和::ReleaseDC()配对

1、今天向客户演示程序,不巧崩溃了。最后发现是使用了GetDC()后没有使用ReleaseDC()引起的。

2、最早听说GetDC()和ReleaseDC()是从前任带头码哥那里听说的,那时候仅仅用《VC 6.0 应用指南》《深入浅出MFC》

      《从入门到精通》等书籍,最多就是有前卫人士从网上下载个代码、从别处拷贝个代码过来看看,从来没听说过msdn

      尽管这样说,无奈我从来没有当真过。今天就被自己给碰到了。

3、网上查了查,还有人说的更玄虚:

      A、::GetDC()和::ReleaseDC()配对不会产生资源泄漏;

      B、GetDC()和ReleaseDC()配对可能会产生资源泄漏。

      唉,宁可信其有,不可信其无啊!跟踪了一下,发现GetDC()还真不简单,还是::GetDC()简单。

4、查查msdn,相关例如以下描写叙述:

Note that the handle to the DC can only be used by a single thread at any one time.
After painting with a common DC, the ReleaseDC function must be called to release the DC. Class and private DCs do not have to be released. ReleaseDC must be called from the same thread that called GetDC. The number of DCs is limited only by available memory.
      首先是强调单线程使用。单线程内部配对调用GetDC和Release。DC的数量收可用内存的限制。
标记一下,以免忘记。唉,不看msdn的人那。
原文地址:https://www.cnblogs.com/zfyouxi/p/4085890.html