Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CDC memory leak

CDC memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Tom_
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • _ _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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      _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++

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups