ReleaseDC和DeleteDC的区别

今天做一个小软件时需要在界面上重复绘制图形

于是写了个函数绘制图形

然后在OnPaint()里调用

发现低频率的重绘窗口还好 一旦经常重绘窗口就会出现错误

应该是内存泄露的错误

但是我发现我所有的CDC全都DeleteDC了 怎么会出现这错误了 

于是查了下MSDN

在ReleaseDC里写着

API原文

intReleaseDC(CDC*pDC);

Return Value

Nonzero if successful; otherwise 0.

Parameters

pDC

Identifies the device context to be released.

Remarks

Releases a device context, freeing it for use by other applications. The effect of the ReleaseDCmember function depends on the device-context type. 

The application must call the ReleaseDCmember function for each call to the member function and for each call to the member function.

这里最后一句提到了

The application must call the ReleaseDCmember function for each call to the member function and for each call to the member function.

所有调用GetWindowDC和GetDC获取的DC对象必须使用ReleaseDC来释放

 

然后又查看了DeleteDC的API内容

 CDC::DeleteDC 

virtualBOOLDeleteDC();

Return Value

Nonzero if the function completed successfully; otherwise 0.

Remarks

In general, do not call this function; the destructor will do it for you. The DeleteDCmember function deletes the Windows device contexts that are associated with m_hDCin the current CDCobject. If this CDCobject is the last active device context for a given device, the device is notified and all storage and system resources used by the device are released. 

An application should not call DeleteDCif objects have been selected into the device context. Objects must first be selected out of the device context before it it is deleted. 

An application must not delete a device context whose handle was obtained by calling . Instead, it must call to free the device context. The and classes are provided to wrap this functionality. 

The DeleteDCfunction is generally used to delete device contexts created with , , or .

 

看了最后一句就可以区分什么时候调用ReleaseDC了

就是GetDC或GetWindowDC获得的DC要使用ReleaseDC释放

Create得到的DC要用Delete释放

这样就不会出现内存泄露事情了 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值