[语法有关问题]这种情况不需要释放对象吗
[语法问题]这种情况不需要释放对象吗?本帖最后由 power17311 于 2013-03-14 00:46:20 编辑看视频教程,有这
[语法问题]这种情况不需要释放对象吗?
本帖最后由 power17311 于 2013-03-14 00:46:20 编辑 看视频教程,有这样一段
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClient dc(this);
CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush = dc.SelectObject(pBrush);
dc.Rectangle(CRect(m_ptHist, point));
dc.SelectObject(pOldBrush);
CView::OnLButtonUp(nFlags, point);
}
我的问题就是, pBrush 所指向的对象,在什么时候释放呢?
我是这样想的, CBrush::FromHandle() 函数返回了一个指向对象的指针,这个函数肯定是使用 new 来创建这个对象的吧?而且走出这个函数,这个对象仍然要用,所以在 CBrush::FromHandle() 函数内部是不能把这个对象释放的。
这样想的话, pBrush 指向的这个对象就一直有效啊,为什么不在最后添一句 pBrush->DeleteObject(); 呢? 释放对象
[解决办法]是临时的 会自动释放!
[解决办法]If a CBrush object is not already attached to the handle, a temporary CBrush object is created and attached. This temporary CBrush object is valid only until the next time the application has idle time in its event loop. At this time, all temporary graphic objects are deleted. In other words, the temporary object is valid only during the processing of one window message
它在这个时候释放:
This temporary CBrush object is valid only until the next time the application has idle time in its event loop。
通俗的是这句:
In other words, the temporary object is valid only during the processing of one window message