Freeing GDI Resources
-
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?
-
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?
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
-
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
-
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
-
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
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.
-
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.
can you post the line-only code? -c
Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten
-
can you post the line-only code? -c
Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten
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,
-
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,
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
-
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
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.
-
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.
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
-
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?
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 -
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?