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. Freeing GDI Resources

Freeing GDI Resources

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsperformancequestion
12 Posts 4 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
    Stew
    wrote on last edited by
    #1

    My app is crashing on machines that aren't equipped with a lot of memory due to the fact that it is eating up all of the system's GDI resources and, apparently, not freeing them. I'm trying to figure out what I'm doing wrong. I've tried DeleteObject after creating a CPen, selecting it into the DC, using it, and selecting the old pen back into the DC as I have read on this message board and in the MSDN library and it hasn't worked. I've also tried using the CAutoPen class that I found on this site for creating a CPen, selecting it into the DC, and selecting the old pen back into the DC when the pen goes out of scope. Nothing seems to be working to give back the GDI resources. Does anyone have any ideas as far as what they think I might be missing?

    C M C 3 Replies Last reply
    0
    • S Stew

      My app is crashing on machines that aren't equipped with a lot of memory due to the fact that it is eating up all of the system's GDI resources and, apparently, not freeing them. I'm trying to figure out what I'm doing wrong. I've tried DeleteObject after creating a CPen, selecting it into the DC, using it, and selecting the old pen back into the DC as I have read on this message board and in the MSDN library and it hasn't worked. I've also tried using the CAutoPen class that I found on this site for creating a CPen, selecting it into the DC, and selecting the old pen back into the DC when the pen goes out of scope. Nothing seems to be working to give back the GDI resources. Does anyone have any ideas as far as what they think I might be missing?

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      are you using other GDI resources, besides CPen (CBrush, CBitmap, etc) ? -c


      Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

      image effects!

      S 1 Reply Last reply
      0
      • C Chris Losinger

        are you using other GDI resources, besides CPen (CBrush, CBitmap, etc) ? -c


        Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

        image effects!

        S Offline
        S Offline
        Stew
        wrote on last edited by
        #3

        I'm using CPen, CFont, and CRgn

        C 1 Reply Last reply
        0
        • S Stew

          I'm using CPen, CFont, and CRgn

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          are you doing the same : CGDIObejct *pOld = dc.SelectObejct(&newObj); ... dc.SelectObject(pOld); newObj.DestroyObject(); sequence for all of them? -c


          Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

          image effects!

          S 1 Reply Last reply
          0
          • C Chris Losinger

            are you doing the same : CGDIObejct *pOld = dc.SelectObejct(&newObj); ... dc.SelectObject(pOld); newObj.DestroyObject(); sequence for all of them? -c


            Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

            image effects!

            S Offline
            S Offline
            Stew
            wrote on last edited by
            #5

            I'm not selecting the region into the DC or deleting it or anything. You think that might be my issue? The only thing is. I've whittled down my app, for testing to find this problem, to not do anything except let me draw a line. In the line class, I have the selectobject and deleteobject calls for the pen and it still eats the GDI memory like crazy because I redraw it each time I mouse over it. But the function for drawing the line when I first draw it or when I mouse over it and off of it is the same function. I'm not sure what I'm missing, but it's obviously something.

            C 1 Reply Last reply
            0
            • S Stew

              I'm not selecting the region into the DC or deleting it or anything. You think that might be my issue? The only thing is. I've whittled down my app, for testing to find this problem, to not do anything except let me draw a line. In the line class, I have the selectobject and deleteobject calls for the pen and it still eats the GDI memory like crazy because I redraw it each time I mouse over it. But the function for drawing the line when I first draw it or when I mouse over it and off of it is the same function. I'm not sure what I'm missing, but it's obviously something.

              C Offline
              C Offline
              Chris Losinger
              wrote on last edited by
              #6

              can you post the line-only code? -c


              Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

              image effects!

              S 1 Reply Last reply
              0
              • C Chris Losinger

                can you post the line-only code? -c


                Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

                image effects!

                S Offline
                S Offline
                Stew
                wrote on last edited by
                #7

                Here is the Constructor: CLine::CLine(CPoint Start, CPoint End, COLORREF aColor, int aWidth, int aStyle, int aType, int aRouteType) { m_StartPoint = Start; m_EndPoint = End; m_Color = aColor; m_Width = aWidth; m_Style = aStyle; m_StartPointRect = (0,0,0,0); m_EndPointRect = (0,0,0,0); m_Type = aType; m_RouteType = aRouteType; m_StartPointRect = CRect(m_StartPoint.x-3, m_StartPoint.y-3, m_StartPoint.x+3, m_StartPoint.y+3); m_EndPointRect = CRect(m_EndPoint.x-3, m_EndPoint.y-3, m_EndPoint.x+3, m_EndPoint.y+3); TermRectRgnPts[0] = CPoint(m_EndPoint.x, m_EndPoint.y - RADIUS); TermRectRgnPts[1] = CPoint(m_EndPoint.x + RADIUS, m_EndPoint.y); TermRectRgnPts[2] = CPoint(m_EndPoint.x, m_EndPoint.y + RADIUS); TermRectRgnPts[3] = CPoint(m_EndPoint.x - RADIUS, m_EndPoint.y); m_RouteTermRect = CRect(m_EndPoint.x - RADIUS, m_EndPoint.y - RADIUS, m_EndPoint.x + RADIUS, m_EndPoint.y + RADIUS); m_EnclosingRect = CRect(Start, End); m_EnclosingRect.NormalizeRect(); m_StartPtScreenPct.XPercent = 0.0; m_StartPtScreenPct.YPercent = 0.0; m_EndPtScreenPct.XPercent = 0.0; m_EndPtScreenPct.YPercent = 0.0; } Here is the Draw Function: void CLine::Draw(CDC* pDC, int aStyle, int aWidth, CElement* pElement, int aRouteType) { //Create a pen and initialize it to the object color and line width of 1 pixel CPen aPen; COLORREF aColor = m_Color; m_DC = pDC; if(this == pElement) { aColor = SELECT_COLOR; m_Style = aStyle; m_Width = aWidth; //m_Width = 0; } if(!aPen.CreatePen(PS_SOLID, m_Width, aColor)) { // Pen creation failed. Abort the program AfxMessageBox("Pen creation failed drawing a line", MB_OK); AfxAbort(); } unsigned Type1[4]; int c1 = GetPattern(Type1, true, m_Width, aStyle); CPen *pOldPen; SetPattern(Type1, c1); { // do drawing inside path for win95/8 compatibility pDC->BeginPath(); MoveTo(m_StartPoint); LineTo(m_EndPoint); pDC->EndPath(); LOGBRUSH lbrush; lbrush.lbStyle = BS_SOLID; lbrush.lbColor = aColor; CPen Pen(PS_GEOMETRIC | PS_SOLID | (true ? (PS_JOIN_ROUND | PS_ENDCAP_ROUND): (PS_JOIN_MITER | PS_ENDCAP_FLAT) ) , m_Width, &lbrush); pOldPen = m_DC->SelectObject(&Pen); pDC->StrokePath(); pDC->SelectObject(pOldPen); aPen.DeleteObject(); // make pen and stroke path //DrawPathOutline(aColor); } pOldPen = pDC->SelectObject(&aPen); /*pDC->MoveTo(m_StartPoint); pDC->LineTo(m_EndPoint);*/ if (aRouteType != 0) { int Xv,

                C 1 Reply Last reply
                0
                • S Stew

                  Here is the Constructor: CLine::CLine(CPoint Start, CPoint End, COLORREF aColor, int aWidth, int aStyle, int aType, int aRouteType) { m_StartPoint = Start; m_EndPoint = End; m_Color = aColor; m_Width = aWidth; m_Style = aStyle; m_StartPointRect = (0,0,0,0); m_EndPointRect = (0,0,0,0); m_Type = aType; m_RouteType = aRouteType; m_StartPointRect = CRect(m_StartPoint.x-3, m_StartPoint.y-3, m_StartPoint.x+3, m_StartPoint.y+3); m_EndPointRect = CRect(m_EndPoint.x-3, m_EndPoint.y-3, m_EndPoint.x+3, m_EndPoint.y+3); TermRectRgnPts[0] = CPoint(m_EndPoint.x, m_EndPoint.y - RADIUS); TermRectRgnPts[1] = CPoint(m_EndPoint.x + RADIUS, m_EndPoint.y); TermRectRgnPts[2] = CPoint(m_EndPoint.x, m_EndPoint.y + RADIUS); TermRectRgnPts[3] = CPoint(m_EndPoint.x - RADIUS, m_EndPoint.y); m_RouteTermRect = CRect(m_EndPoint.x - RADIUS, m_EndPoint.y - RADIUS, m_EndPoint.x + RADIUS, m_EndPoint.y + RADIUS); m_EnclosingRect = CRect(Start, End); m_EnclosingRect.NormalizeRect(); m_StartPtScreenPct.XPercent = 0.0; m_StartPtScreenPct.YPercent = 0.0; m_EndPtScreenPct.XPercent = 0.0; m_EndPtScreenPct.YPercent = 0.0; } Here is the Draw Function: void CLine::Draw(CDC* pDC, int aStyle, int aWidth, CElement* pElement, int aRouteType) { //Create a pen and initialize it to the object color and line width of 1 pixel CPen aPen; COLORREF aColor = m_Color; m_DC = pDC; if(this == pElement) { aColor = SELECT_COLOR; m_Style = aStyle; m_Width = aWidth; //m_Width = 0; } if(!aPen.CreatePen(PS_SOLID, m_Width, aColor)) { // Pen creation failed. Abort the program AfxMessageBox("Pen creation failed drawing a line", MB_OK); AfxAbort(); } unsigned Type1[4]; int c1 = GetPattern(Type1, true, m_Width, aStyle); CPen *pOldPen; SetPattern(Type1, c1); { // do drawing inside path for win95/8 compatibility pDC->BeginPath(); MoveTo(m_StartPoint); LineTo(m_EndPoint); pDC->EndPath(); LOGBRUSH lbrush; lbrush.lbStyle = BS_SOLID; lbrush.lbColor = aColor; CPen Pen(PS_GEOMETRIC | PS_SOLID | (true ? (PS_JOIN_ROUND | PS_ENDCAP_ROUND): (PS_JOIN_MITER | PS_ENDCAP_FLAT) ) , m_Width, &lbrush); pOldPen = m_DC->SelectObject(&Pen); pDC->StrokePath(); pDC->SelectObject(pOldPen); aPen.DeleteObject(); // make pen and stroke path //DrawPathOutline(aColor); } pOldPen = pDC->SelectObject(&aPen); /*pDC->MoveTo(m_StartPoint); pDC->LineTo(m_EndPoint);*/ if (aRouteType != 0) { int Xv,

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #8

                  Stew wrote: LOGBRUSH lbrush; lbrush.lbStyle = BS_SOLID; lbrush.lbColor = aColor; CPen Pen(PS_GEOMETRIC | PS_SOLID | (true ? (PS_JOIN_ROUND | PS_ENDCAP_ROUND): (PS_JOIN_MITER | PS_ENDCAP_FLAT) ) , m_Width, &lbrush); pOldPen = m_DC->SelectObject(&Pen); pDC->StrokePath(); pDC->SelectObject(pOldPen); aPen.DeleteObject(); here, you're creating and selecting "Pen", but doing a DeleteObject on aPen. ? -c


                  Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

                  image effects!

                  S 1 Reply Last reply
                  0
                  • C Chris Losinger

                    Stew wrote: LOGBRUSH lbrush; lbrush.lbStyle = BS_SOLID; lbrush.lbColor = aColor; CPen Pen(PS_GEOMETRIC | PS_SOLID | (true ? (PS_JOIN_ROUND | PS_ENDCAP_ROUND): (PS_JOIN_MITER | PS_ENDCAP_FLAT) ) , m_Width, &lbrush); pOldPen = m_DC->SelectObject(&Pen); pDC->StrokePath(); pDC->SelectObject(pOldPen); aPen.DeleteObject(); here, you're creating and selecting "Pen", but doing a DeleteObject on aPen. ? -c


                    Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

                    image effects!

                    S Offline
                    S Offline
                    Stew
                    wrote on last edited by
                    #9

                    Minor mishap. With all the things I've been trying, I missed that. At any rate, I changed it and it's still usurping all of the GDI resources and not freeing them. Let me ask you this, do cursors take up GDI resources? I don't think they do, but I'm not sure.

                    C 1 Reply Last reply
                    0
                    • S Stew

                      Minor mishap. With all the things I've been trying, I missed that. At any rate, I changed it and it's still usurping all of the GDI resources and not freeing them. Let me ask you this, do cursors take up GDI resources? I don't think they do, but I'm not sure.

                      C Offline
                      C Offline
                      Chris Losinger
                      wrote on last edited by
                      #10

                      i don't think so. this is probably something really simple, but easy to overlook... -c


                      Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

                      image effects!

                      1 Reply Last reply
                      0
                      • S Stew

                        My app is crashing on machines that aren't equipped with a lot of memory due to the fact that it is eating up all of the system's GDI resources and, apparently, not freeing them. I'm trying to figure out what I'm doing wrong. I've tried DeleteObject after creating a CPen, selecting it into the DC, using it, and selecting the old pen back into the DC as I have read on this message board and in the MSDN library and it hasn't worked. I've also tried using the CAutoPen class that I found on this site for creating a CPen, selecting it into the DC, and selecting the old pen back into the DC when the pen goes out of scope. Nothing seems to be working to give back the GDI resources. Does anyone have any ideas as far as what they think I might be missing?

                        M Offline
                        M Offline
                        Mustafa
                        wrote on last edited by
                        #11

                        Hi Stew, I am currently facing the same problem...did you solve it??. I noticed that GDI resource are returned properly to system after calling StrokePath() or StrokeAndFillPath(). My code pDC->BeginPath();// GDI used pDC->MoveTo(some point); pDC->LineTo(another point); pDC->EndPath(); pDC->WidenPath(); HRGN hRgn=::PathToRegion(pDC->GetSafeHdc()); ::FillRgn(pDC->GetSafeHdc(),m_hLastSecondRgn,(HBRUSH)GreyBrush); This code doesn't return GDI resource...please help cause I have been stuck on this for quite some time now... Regards Mustafa

                        1 Reply Last reply
                        0
                        • S Stew

                          My app is crashing on machines that aren't equipped with a lot of memory due to the fact that it is eating up all of the system's GDI resources and, apparently, not freeing them. I'm trying to figure out what I'm doing wrong. I've tried DeleteObject after creating a CPen, selecting it into the DC, using it, and selecting the old pen back into the DC as I have read on this message board and in the MSDN library and it hasn't worked. I've also tried using the CAutoPen class that I found on this site for creating a CPen, selecting it into the DC, and selecting the old pen back into the DC when the pen goes out of scope. Nothing seems to be working to give back the GDI resources. Does anyone have any ideas as far as what they think I might be missing?

                          C Offline
                          C Offline
                          cmk
                          wrote on last edited by
                          #12

                          I am pretty sure MFC does a lot of caching which will make it that much harder to determine if/where you have a leak as the memory will not be releasing when you think it should. May want to try straight Win32 first - if you haven't already. ...cmk

                          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