ccnuxjg 2002年11月25日
超难题:为什么在Win98下,频繁的使用CreatePen,DeleteObject,SelectObject会出现资源泄漏!?
在98下,在OnTimer里面使用下面的函数会有资源泄漏,为什么呀。
void DrawGraphyClass::DrawGraphy ( CDC *pDC, const int _InputRate )
{

CDC MemDC;
CBitmap MemBitmap;
MemDC.CreateCompatibleDC ( pDC );

static width = m_DisplayRect.right - m_DisplayRect.left;
static height = m_DisplayRect.bottom - m_DisplayRect.top;
MemBitmap.CreateCompatibleBitmap ( pDC, width, height );

CBitmap *pOldBit=(CBitmap *)MemDC.SelectObject(&MemBitmap);
ASSERT (pOldBit != NULL);

MemDC.FillSolidRect (0, 0, width, height, RGB(0, 0, 0));
CPen pen,pen2;
CPen *OldPen;
pen.CreatePen(PS_SOLID, 1, m_GridColor ) ;
OldPen = (CPen *)MemDC.SelectObject(&pen);

static int count = 0;
count ++;
int _Interval = m_nInterval / m_nTimes;
for ( int i = 1; i <= m_nHorNum; i++ )
{
MemDC.MoveTo ( 0, i * m_nInterval );
MemDC.LineTo ( m_DisplayRect.right - m_DisplayRect.left, i * m_nInterval );
}
for ( int j = 1; j <= m_nColNum; j++ )
{
count = count % m_nTimes;
MemDC.MoveTo ( j * m_nInterval - count * _Interval, 0 );
MemDC.LineTo ( j * m_nInterval - count * _Interval, m_DisplayRect.bottom - m_DisplayRect.top );
}

MemDC.SelectObject ( OldPen );
pen.DeleteObject();

pen2.CreatePen (PS_SOLID, 1, m_LineColor );
OldPen = (CPen *)MemDC.SelectObject ( &pen2 );

float _fTempInterval = static_cast<float> ( m_DisplayRect.bottom - m_DisplayRect.top ) / 100 ;
int _yPos = static_cast<int> ( m_DisplayRect.bottom - m_DisplayRect.top - _InputRate * _fTempInterval + 0.5 );

std::list<CPoint>::iterator it;

if ( m_PointList.size() < m_nTotalNum )
{
m_PointList.push_back ( CPoint ( m_DisplayRect.right - m_DisplayRect.left, _yPos ) );
}
else
{
m_PointList.push_back ( CPoint ( m_DisplayRect.right - m_DisplayRect.left, _yPos ) );
m_PointList.pop_front ();
}
for ( it = m_PointList.end(); it != m_PointList.begin(); it-- )
{
it->x = it->x - _Interval;
}
it->x = it->x - _Interval;
int n=0;
for ( it = m_PointList.end(), n=0, it--; it != m_PointList.begin() && n<= m_PointList.size(); it--, n++ )
{
CPoint _tempPoint = *it;
CPoint _TempPoint = *--it;
it++;
MemDC.MoveTo ( _tempPoint );
MemDC.LineTo ( _TempPoint );
}


pDC->BitBlt ( m_DisplayRect.left, m_DisplayRect.top, m_DisplayRect.right-m_DisplayRect.left, m_DisplayRect.bottom-m_DisplayRect.top, &MemDC, 0, 0, SRCCOPY );
MemDC.SelectObject ( OldPen);
pen2.DeleteObject();

MemDC.SelectObject(pOldBit);
MemBitmap.DeleteObject();
MemDC.DeleteDC();
}
...全文
54 点赞 收藏 25
写回复
25 条回复

还没有回复,快来抢沙发~

发动态
发帖子
VC/MFC
创建于2007-09-28

7872

社区成员

42.1w+

社区内容

VC/MFC相关问题讨论
社区公告
暂无公告