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. NT...Win9X...problem.....memory leak???

NT...Win9X...problem.....memory leak???

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++graphicsquestionperformance
3 Posts 3 Posters 1 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, Thanks for all of your reply about my question before. THANK YOU VERY MUCH!!!! I try to keep posting my question, so that someone new can see this question and tell me how to fix it.... My major problem is: I have a program that was implemented in NT before and now want to transfer to Win9x. Everything is fine in NT, but in Win9x after the program has run for awhile, the output font change...include the system message box ( i know it because there is a message box ask me to terminate the program..haha..)......but it takes awhile to see the problem........when i do drawing....the problem happened immediately.....( not just draw a line......but a heavy drawing....).....cannot display colour....etc... I posted this question in codeproject.....and many people told me it would be a GDI resource leak....but i don't know how to fix it.... Oh...give you more backgrounds......it is a program using MFC...........and has a CDC global pointer to let other files to do drawing too...(not in view.cpp only....).....I'm just used some liked CPen or CBrush...then selectobject.....then something liked LineTo or MoveTo..... Thanks for your help..... Richard

    T 1 Reply Last reply
    0
    • L Lost User

      Hi, Thanks for all of your reply about my question before. THANK YOU VERY MUCH!!!! I try to keep posting my question, so that someone new can see this question and tell me how to fix it.... My major problem is: I have a program that was implemented in NT before and now want to transfer to Win9x. Everything is fine in NT, but in Win9x after the program has run for awhile, the output font change...include the system message box ( i know it because there is a message box ask me to terminate the program..haha..)......but it takes awhile to see the problem........when i do drawing....the problem happened immediately.....( not just draw a line......but a heavy drawing....).....cannot display colour....etc... I posted this question in codeproject.....and many people told me it would be a GDI resource leak....but i don't know how to fix it.... Oh...give you more backgrounds......it is a program using MFC...........and has a CDC global pointer to let other files to do drawing too...(not in view.cpp only....).....I'm just used some liked CPen or CBrush...then selectobject.....then something liked LineTo or MoveTo..... Thanks for your help..... Richard

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      I posted this advice before, and I repeat it: you should get rid of the global DC. Just pass the CDC pointer from OnDraw to functions defined in other .cpp files. Your GDI leaks are (most probably) caused by the fact that pens, brushes and other GDI objects can't be freed when selected into any device context.

      void Leak(CDC *pDC)
      {
      CPen pen(...);
      pDC->SelectObject(&pen);
      // ... draw here
      }

      CPen's destructor will call ::DeleteObject for you, but it will fail - your pen is still selected. You should re-select before exiting the function:

      void TheRightStuff(CDC *pDC)
      {
      CPen pen(...);
      CPen *oldPen = pDC->SelectObject(&pen);
      // ... draw here
      pDC->SelectObject(oldPen);
      }

      Tomasz Sowinski -- http://www.shooltz.com.plC

      R 1 Reply Last reply
      0
      • T Tomasz Sowinski

        I posted this advice before, and I repeat it: you should get rid of the global DC. Just pass the CDC pointer from OnDraw to functions defined in other .cpp files. Your GDI leaks are (most probably) caused by the fact that pens, brushes and other GDI objects can't be freed when selected into any device context.

        void Leak(CDC *pDC)
        {
        CPen pen(...);
        pDC->SelectObject(&pen);
        // ... draw here
        }

        CPen's destructor will call ::DeleteObject for you, but it will fail - your pen is still selected. You should re-select before exiting the function:

        void TheRightStuff(CDC *pDC)
        {
        CPen pen(...);
        CPen *oldPen = pDC->SelectObject(&pen);
        // ... draw here
        pDC->SelectObject(oldPen);
        }

        Tomasz Sowinski -- http://www.shooltz.com.plC

        R Offline
        R Offline
        Richard Cheng
        wrote on last edited by
        #3

        Oh.....i understand that now......let me try.... Thanks, Tomaxz....... THANK YOU THANK YOU THANK YOU!!!!!;P

        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