Remember to delete the Object you have created by DeleteObject() function. If forgetting it, your application may breakdown after a while.
The DeleteObject function deletes a logical pen, brush, font, bitmap, region, or palette, freeing all system resources associated with the object. After the object is deleted, the specified handle is no longer valid. (MSDN).
Here we get a example to illustrate it..
The source program is get from Windows Programming(Charles Petzold) and the program aim to draw rectangles randomly. Here is the code section:
……
while (TRUE)
{
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break ;
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
else
DrawRectangle (hwnd) ;
}
…..
void DrawRectangle (HWND hwnd)
{
HBRUSHhBrush ;
HDC hdc ;
RECT rect ;
if (cxClient == 0 || cyClient == 0)
return ;
SetRect (&rect, rand () % cxClient, rand () % cyClient,
rand () % cxClient, rand () % cyClient) ;
hBrush = CreateSolidBrush ( RGB (rand () % 256, rand () % 256, rand () % 256)) ;
hdc = GetDC (hwnd) ;
FillRect (hdc, &rect, hBrush) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
}
……
The program uses the idle time of disposing message to draw rectangles. DeleteObject (hBrush) ; is significance to this program. If we remove it ,the program will work out of the way after several seconds. The objects you have created may occupy system resources and cause something wrong.