how do i use DeleteObject()...
-
do i use delete object like this to prevent resource leaks??? // Destructor CBoard::~CBoard() { // Delete the object to prevent resource leaks mpPen->DeleteObject(); mpFont->DeleteObject(); // Destroys all the pointers to all objects delete mpPen; delete mpFont; for (int r = 0; r < MAXROW; r++) for (int c= 0; c < MAXCOL; c++) delete mpGrid[r][c]; } or i can just destroy it by deleting it or do i have to release it before i delete it when i called it like this: // Change the Font Height and Width to the values entered void CBoard::SetFontSize(CDC* pDC, int Height, int Width) { mpFont = new CFont; VERIFY(mpFont->CreateFont(Height,Width,0,0,0,0,0,0,0,0,0,0, DEFAULT_PITCH|FF_DONTCARE,"Tictactoe")); VERIFY(mpDefaultFont = pDC->SelectObject(mpFont)); } anyways, can anyone tell me if i am doing it right or what i am doing can cause memory or resource leaks... well, thank you in advance for your help :) :) John
-
do i use delete object like this to prevent resource leaks??? // Destructor CBoard::~CBoard() { // Delete the object to prevent resource leaks mpPen->DeleteObject(); mpFont->DeleteObject(); // Destroys all the pointers to all objects delete mpPen; delete mpFont; for (int r = 0; r < MAXROW; r++) for (int c= 0; c < MAXCOL; c++) delete mpGrid[r][c]; } or i can just destroy it by deleting it or do i have to release it before i delete it when i called it like this: // Change the Font Height and Width to the values entered void CBoard::SetFontSize(CDC* pDC, int Height, int Width) { mpFont = new CFont; VERIFY(mpFont->CreateFont(Height,Width,0,0,0,0,0,0,0,0,0,0, DEFAULT_PITCH|FF_DONTCARE,"Tictactoe")); VERIFY(mpDefaultFont = pDC->SelectObject(mpFont)); } anyways, can anyone tell me if i am doing it right or what i am doing can cause memory or resource leaks... well, thank you in advance for your help :) :) John
The most important step is selecting the original pen/brush/font into the DC before destroying it. OTherwise you will leak GDI resources and your program will eventually kill the graphics subsystem on any non-NT based OS. You don't need to call DeleteObject before calling delete. Delete allows you to delete the handle being encapsulated without destroying the class object. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little.
-
The most important step is selecting the original pen/brush/font into the DC before destroying it. OTherwise you will leak GDI resources and your program will eventually kill the graphics subsystem on any non-NT based OS. You don't need to call DeleteObject before calling delete. Delete allows you to delete the handle being encapsulated without destroying the class object. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little.