Article ID: 151865
Article Last Modified on 12/1/2003
CBrush *pBrush1 = new CBrush (RGB(255,0,0));
CBrush *pBrush2 = new CBrush (RGB(255,0,0));
CBrush *pOld = pDC->SelectObject(pBrush1);
// Do some stuff in here
// Don't rely on the return value from SelectObject being pBrush1
pDC->SelectObject(pOld);
// Instead, maintain the pointer to the object yourself
delete pBrush1;
The following functions might return an unexpected object:
SelectObject
FromHandle
FromHandlePermanent
GetCurrentBrush
GetHalftoneBrush
SelectStockObject
HANDLE hBrush = ::CreateSolidBrush(RGB(255,0,0)); HANDLE hBrush2 = ::CreateSolidBrush(RGB(255,0,0)); // In this case, hBrush and hBrush2 will have different values on // Windows NT or Windows 3.x, so the following assert will succeed ASSERT(hBrush!=hBrush2);On Windows 95, the above assert will fail. Windows 95 is optimized to detect when an application is creating a duplicate brush handle so it doesn't need to create a brand new brush, just maintain internal information about the brush and return the same handle.
CBrush *pBrush1 = new CBrush (RGB(255,0,0));
CBrush *pBrush2 = new CBrush (RGB(255,0,0));
CBrush *pOld = pDC->SelectObject(pBrush1);
// Do some stuff in here
delete pDC->SelectObject(pOld);
ASSERT_VALID(pBrush2);
// On Windows NT or Windows 3.x, the above call to "delete"
// will delete pBrush1. On Windows 95, the above call to "delete"
// will delete pBrush2 and the ASSERT_VALID will fail.
Additional query words: 1.00 1.50 1.51 1.52 2.00 2.10 2.20 4.00 4.10
Keywords: kbuidesign kbarchitecture kbprb kbbrush KB151865