CDC memory leak
-
Hi all, I'm stuggling to resolve a memory leak. The code below gets updated every few seconds and changes the image displayed depending on the state of m_bOn.. CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC ); if ( pDC ) { CRect rect = lpDrawItemStruct->rcItem; int nSavedDC = pDC->SaveDC(); pDC->SelectStockObject( NULL_BRUSH ); pDC->FillSolidRect( rect, ::GetSysColor( COLOR_BTNFACE ) ); if( m_bOn ) { pDC->BitBlt( m_ptCentre.x, m_ptCentre.y, m_nWidth, m_nHeight, &m_dcMem, 1 * m_nWidth, 0, SRCCOPY ); } else { pDC->BitBlt( m_ptCentre.x, m_ptCentre.y, m_nWidth, m_nHeight, &m_dcMem, 0 * m_nWidth, 0, SRCCOPY ); } pDC->RestoreDC( nSavedDC ); } I know the leak is here somewhere but I can't see what would be wrong! I'd really appreciate some help or some pointers in the right direction. Many thanks Tom
-
Hi all, I'm stuggling to resolve a memory leak. The code below gets updated every few seconds and changes the image displayed depending on the state of m_bOn.. CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC ); if ( pDC ) { CRect rect = lpDrawItemStruct->rcItem; int nSavedDC = pDC->SaveDC(); pDC->SelectStockObject( NULL_BRUSH ); pDC->FillSolidRect( rect, ::GetSysColor( COLOR_BTNFACE ) ); if( m_bOn ) { pDC->BitBlt( m_ptCentre.x, m_ptCentre.y, m_nWidth, m_nHeight, &m_dcMem, 1 * m_nWidth, 0, SRCCOPY ); } else { pDC->BitBlt( m_ptCentre.x, m_ptCentre.y, m_nWidth, m_nHeight, &m_dcMem, 0 * m_nWidth, 0, SRCCOPY ); } pDC->RestoreDC( nSavedDC ); } I know the leak is here somewhere but I can't see what would be wrong! I'd really appreciate some help or some pointers in the right direction. Many thanks Tom
_Tom_ wrote:
pDC->SelectStockObject( NULL_BRUSH );
Does this have a return value ? When you select your own brush, a pointer to the old brush is returned and you need to grab it and select it back in to avoid a memory leak. Christian Graus - Microsoft MVP - C++