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. CEditView -> CEdit::ReplaceSel() leaks GDI Objects?

CEditView -> CEdit::ReplaceSel() leaks GDI Objects?

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsarchitecturehelpquestiondiscussion
4 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.
  • S Offline
    S Offline
    scott sanders
    wrote on last edited by
    #1

    I have an MDI app using CEditView in the Doc/View architecture. In this view, I have a function like so, which I call from the Document class: void CMyEditView::addText(LPCTSTR szOutput) { ASSERT_VALID(this); int nSize = GetBufferLength(); GetEditCtrl().SetSel(nSize, nSize, TRUE); GetEditCtrl().ReplaceSel(szOutput, FALSE); } After feeding it data for a while, the program inevitably crashes and the display gets corrupted. By watching the executable's resources in Task Manager, I notice that the GDI Objects are steadily increasing, until they reach 9999, and then the crash happens. By setting breakpoints, I can see that one GDI Object is consumed every time CEdit::ReplaceSel() is called, and never released. Is this method for appending text to an Edit Control contained in a View appropriate? Anyone know a better way? Using GetWindowText() and SetWindowText() has the same problem! Any thoughts as to what's happening, and how I can prevent the resource loss? Thanks ! scott sanders

    H 1 Reply Last reply
    0
    • S scott sanders

      I have an MDI app using CEditView in the Doc/View architecture. In this view, I have a function like so, which I call from the Document class: void CMyEditView::addText(LPCTSTR szOutput) { ASSERT_VALID(this); int nSize = GetBufferLength(); GetEditCtrl().SetSel(nSize, nSize, TRUE); GetEditCtrl().ReplaceSel(szOutput, FALSE); } After feeding it data for a while, the program inevitably crashes and the display gets corrupted. By watching the executable's resources in Task Manager, I notice that the GDI Objects are steadily increasing, until they reach 9999, and then the crash happens. By setting breakpoints, I can see that one GDI Object is consumed every time CEdit::ReplaceSel() is called, and never released. Is this method for appending text to an Edit Control contained in a View appropriate? Anyone know a better way? Using GetWindowText() and SetWindowText() has the same problem! Any thoughts as to what's happening, and how I can prevent the resource loss? Thanks ! scott sanders

      H Offline
      H Offline
      Hans Ruck
      wrote on last edited by
      #2

      I created a simple MDI application as you described and - using the same code you wrote - the GDI objects are changing between 31 and 32. It looks ok. Where is that szOutput coming from? rechi

      S 1 Reply Last reply
      0
      • H Hans Ruck

        I created a simple MDI application as you described and - using the same code you wrote - the GDI objects are changing between 31 and 32. It looks ok. Where is that szOutput coming from? rechi

        S Offline
        S Offline
        scott sanders
        wrote on last edited by
        #3

        Thank you for your reply ! After experimenting a bit more, I found out that the resource leak was actually coming from my CMyView::CtlColor() function. I was allocating a brush to change the background color of the window, depending on the program state, and never releasing it. So it was my silly fault all along; sorry to send you on a wild goose chase. I changed the brush to be static, and the problem disappeared. Thanks again for looking into this for me. scott

        H 1 Reply Last reply
        0
        • S scott sanders

          Thank you for your reply ! After experimenting a bit more, I found out that the resource leak was actually coming from my CMyView::CtlColor() function. I was allocating a brush to change the background color of the window, depending on the program state, and never releasing it. So it was my silly fault all along; sorry to send you on a wild goose chase. I changed the brush to be static, and the problem disappeared. Thanks again for looking into this for me. scott

          H Offline
          H Offline
          Hans Ruck
          wrote on last edited by
          #4

          Intersting case, it teaches me a good little lesson. I thank you too! rechi

          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